Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C33-L1294-1E44BF10

Home Dashboard
Chapter 3 · Module 1 — Pandas Fundamentals

Creating and Understanding DataFrames

Priority
  • JobHigh
  • InterviewMedium
  • DevelopmentHigh
4,222
words
24
min read
3
tools

1. Learning Outcomes#

या chapter नंतर तुम्हाला:

  • DataFrame म्हणजे नेमके काय हे समजेल
  • pd.DataFrame() वापरून DataFrame तयार करता येईल
  • Dictionary of Lists मधून DataFrame तयार करता येईल
  • List of Dictionaries मधून DataFrame तयार करता येईल
  • List of Lists मधून DataFrame तयार करता येईल
  • NumPy Array ला DataFrame मध्ये convert करता येईल
  • meaningful column names आणि custom row index labels देता येतील
  • DataFrame मधील rows, columns आणि index यांच्यातील फरक समजेल
  • index, columns, shape, size, dtypes, आणि empty वापरून basic structure inspect करता येईल
  • एका DataFrame मध्ये वेगवेगळ्या columns चे वेगवेगळे data types का असू शकतात हे समजेल
  • DataFrame तयार करताना होणाऱ्या common beginner mistakes ओळखता येतील

2. DataFrame म्हणजे नेमके काय?#

समजा आपल्याकडे students ची माहिती आहे.

name age city
Asha 21 Pune
Ravi 23 Nashik
Meera 22 Mumbai

ही माहिती naturally rows आणि columns मध्ये बसते.

प्रत्येक row एका student ची माहिती दाखवते.

प्रत्येक column एखाद्या specific field ची माहिती दाखवतो.

उदाहरणार्थ:

  • name → student चे नाव
  • age → student चे वय
  • city → student कोणत्या शहरात आहे

Pandas मध्ये अशा two-dimensional tabular data साठी मुख्य object म्हणजे DataFrame.

Key Idea

DataFrame म्हणजे rows आणि columns मध्ये व्यवस्थित मांडलेला labelled tabular data structure.

Spreadsheet किंवा database table पाहिला असेल तर DataFrame ची basic कल्पना पटकन समजते.

पण DataFrame फक्त दिसायला table नाही.

त्याच्याकडे:

  • row labels असतात
  • column labels असतात
  • प्रत्येक column चा data type असतो
  • स्वतःचा आकार म्हणजे shape असतो
  • structure बद्दल माहिती देणारे useful attributes असतात

हेच आपण या chapter मध्ये step-by-step समजून घेणार आहोत.

3. pd.DataFrame() — DataFrame तयार करण्याचा मुख्य constructor#

Pandas मध्ये DataFrame तयार करण्यासाठी आपण pd.DataFrame() वापरतो.

आधी pandas import केलेले असणे आवश्यक आहे.

PYTHON
import pandas as pd

इथे pd हे pandas साठी वापरलेले standard alias आहे.

सर्वात basic DataFrame असा तयार करता येतो:

PYTHON
df = pd.DataFrame()

हा DataFrame तयार झाला आहे, पण त्यामध्ये सध्या rows किंवा columns नाहीत.

म्हणून:

PYTHON
print(df.empty)

यामध्ये df.empty ची value True असते.

पण real-world मध्ये आपण बहुतेक वेळा pd.DataFrame() ला काही data देतो.

Data कुठल्या form मध्ये असू शकतो?

उदाहरणार्थ:

  • Dictionary of Lists
  • List of Dictionaries
  • List of Lists
  • NumPy Array

हे वेगवेगळे inputs एकाच DataFrame object मध्ये convert होऊ शकतात.

Visual Diagram80%

Rendering diagram…

Visual मध्ये महत्त्वाची गोष्ट ही आहे की input ची structure वेगळी असली तरी अंतिम उद्देश एकच आहे:

data ला labelled rows आणि columns असलेल्या DataFrame मध्ये आणणे.

4. Dictionary of Lists मधून DataFrame तयार करणे#

हा DataFrame तयार करण्याचा खूप common आणि beginner-friendly मार्ग आहे.

समजा आपल्याकडे खालील dictionary आहे:

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "age": [21, 23, 22],
    "city": ["Pune", "Nashik", "Mumbai"]
}

इथे प्रत्येक dictionary key एक logical field दाखवत आहे.

  • "name" → names चा column
  • "age" → ages चा column
  • "city" → cities चा column

आता DataFrame तयार करू:

PYTHON
import pandas as pd

data = {
    "name": ["Asha", "Ravi", "Meera"],
    "age": [21, 23, 22],
    "city": ["Pune", "Nashik", "Mumbai"]
}

df = pd.DataFrame(data)

print(df)

यामध्ये dictionary keys DataFrame चे column labels बनतात.

Conceptually DataFrame असा दिसतो:

index name age city
0 Asha 21 Pune
1 Ravi 23 Nashik
2 Meera 22 Mumbai

इथे 0, 1, 2 हे default row index labels आहेत.

आपण ते explicitly दिलेले नाहीत.

Pandas ने ते automatically तयार केले.

Dictionary of Lists चा mental model#

Dictionary असा विचार करा:

column name → त्या column मधील values

उदाहरणार्थ:

"age" या key सोबत [21, 23, 22] ही list आहे.

म्हणजे age column मध्ये तीन values आहेत.

महत्त्वाचे

Dictionary of Lists वापरताना सामान्य rectangular DataFrame तयार करण्यासाठी column lists ची length समान असली पाहिजे.

उदाहरणार्थ हे problematic आहे:

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "age": [21, 23]
}

name मध्ये तीन values आहेत.

पण age मध्ये दोनच values आहेत.

मग Pandas ला तिसऱ्या row च्या age साठी कोणती value वापरायची हे माहिती नाही.

म्हणून अशा mismatched lengths मुळे DataFrame construction fail होऊ शकते.

5. List of Dictionaries मधून DataFrame तयार करणे#

आता data कडे दुसऱ्या प्रकारे पाहू.

Dictionary of Lists मध्ये आपण असा विचार केला:

एक key = एक column

List of Dictionaries मध्ये प्रत्येक dictionary साधारणपणे एक row किंवा record represent करते.

उदाहरण:

PYTHON
students = [
    {"name": "Asha", "age": 21, "city": "Pune"},
    {"name": "Ravi", "age": 23, "city": "Nashik"},
    {"name": "Meera", "age": 22, "city": "Mumbai"}
]

df = pd.DataFrame(students)

इथे:

पहिली dictionary → पहिली row

दुसरी dictionary → दुसरी row

तिसरी dictionary → तिसरी row

Dictionary मधील keys:

  • name
  • age
  • city

हे column labels बनतात.

Conceptually result:

index name age city
0 Asha 21 Pune
1 Ravi 23 Nashik
2 Meera 22 Mumbai

Dictionary of Lists आणि List of Dictionaries मधला फरक#

Input structure सहज mental model
Dictionary of Lists प्रत्येक key एक column
List of Dictionaries प्रत्येक dictionary एक row

दोन्हीमधून शेवटी समान प्रकारचा DataFrame तयार होऊ शकतो.

फरक फक्त source data कसा organised आहे यात आहे.

Practical applications मध्ये API किंवा JSON-style records पासून data मिळाला तर List of Dictionaries खूप natural असू शकते.

6. List of Lists मधून DataFrame तयार करणे#

आता आपल्याकडे data असा आहे:

PYTHON
students = [
    ["Asha", 21, "Pune"],
    ["Ravi", 23, "Nashik"],
    ["Meera", 22, "Mumbai"]
]

इथे प्रत्येक inner list एका row सारखी आहे.

पहिली row:

["Asha", 21, "Pune"]

दुसरी row:

["Ravi", 23, "Nashik"]

तिसरी row:

["Meera", 22, "Mumbai"]

DataFrame तयार करू:

PYTHON
df = pd.DataFrame(students)

DataFrame तयार होईल.

पण एक problem आहे.

Pandas ला "Asha" म्हणजे name, 21 म्हणजे age, आणि "Pune" म्हणजे city हे आपोआप कसे कळणार?

ते कळणार नाही.

म्हणून meaningful column names explicitly देणे चांगले.

PYTHON
df = pd.DataFrame(
    students,
    columns=["name", "age", "city"]
)

आता structure स्पष्ट आहे.

index name age city
0 Asha 21 Pune
1 Ravi 23 Nashik
2 Meera 22 Mumbai
Remember

List of Lists मध्ये inner list साधारणपणे एक row represent करते. त्या values चा अर्थ स्पष्ट करण्यासाठी columns देणे महत्त्वाचे असते.

7. NumPy Array मधून DataFrame तयार करणे — Integration Use#

Data Science मध्ये NumPy आणि Pandas अनेकदा एकत्र वापरले जातात.

समजा NumPy array आधीच तयार आहे:

PYTHON
import numpy as np

scores = np.array([
    [101, 75],
    [102, 88],
    [103, 92]
])

इथे NumPy array मध्ये फक्त values आहेत.

Business meaning अजून स्पष्ट नाही.

पहिला column काय?

दुसरा column काय?

आपल्याला माहीत आहे:

  • पहिला column → student ID
  • दुसरा column → score

म्हणून DataFrame तयार करताना labels देऊ शकतो:

PYTHON
import pandas as pd
import numpy as np

scores = np.array([
    [101, 75],
    [102, 88],
    [103, 92]
])

df = pd.DataFrame(
    scores,
    columns=["student_id", "score"]
)

Conceptually:

index student_id score
0 101 75
1 102 88
2 103 92

NumPy array ने आपल्याला values दिल्या.

Pandas DataFrame ने त्या values भोवती meaningful row/column structure दिली.

Key Idea

NumPy Array → values-oriented structure
DataFrame → values + labels + tabular structure

इथे NumPy पुन्हा शिकण्याची गरज नाही.

आपल्याला फक्त एवढे समजायचे आहे की existing NumPy data ला pd.DataFrame() मध्ये input म्हणून देता येते.

8. Column Names देणे#

Column म्हणजे vertical field.

उदाहरणार्थ:

name age city
Asha 21 Pune
Ravi 23 Nashik

इथे:

  • name
  • age
  • city

ही column names आहेत.

List of Lists किंवा NumPy Array सारख्या inputs मध्ये meaningful names explicit देण्यासाठी columns parameter वापरता येतो.

PYTHON
data = [
    ["Pen", 20],
    ["Notebook", 60],
    ["Bag", 900]
]

df = pd.DataFrame(
    data,
    columns=["product", "price"]
)

columns=["product", "price"] याचा अर्थ:

पहिली value → product

दुसरी value → price

Column names चांगली असतील तर पुढचे analysis समजणे खूप सोपे होते.

0, 1, 2 अशा default labels पेक्षा product, price, quantity अशा names चा अर्थ लगेच समजतो.

9. Custom Row Index Labels देणे#

Columns ना names असतात.

त्याचप्रमाणे rows ना देखील labels असू शकतात.

त्या row labels च्या collection ला DataFrame चा index म्हणतात.

Default DataFrame मध्ये index बहुतेक वेळा असा दिसतो:

0, 1, 2, ...

पण आपल्याला custom labels हवे असतील तर index parameter वापरता येतो.

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "score": [75, 88, 92]
}

df = pd.DataFrame(
    data,
    index=["student_1", "student_2", "student_3"]
)

Conceptually:

index name score
student_1 Asha 75
student_2 Ravi 88
student_3 Meera 92

इथे student_1, student_2, student_3 या actual data columns नाहीत.

त्या rows चे labels आहेत.

हा फरक खूप महत्त्वाचा आहे.

Common Mistake

index म्हणजे DataFrame मधील एक normal data column असे नेहमी समजू नका. Index हा rows ला identify करणारा स्वतंत्र label system आहे.

Custom index labels ची संख्या rows च्या संख्येशी जुळली पाहिजे.

तीन rows असतील तर तीन index labels देणे अपेक्षित आहे.

10. DataFrame चे Rows, Columns आणि Index#

आता DataFrame चे anatomy visually पाहू.

खालील visual मध्ये Index नावाचा left-side helper column फक्त row labels समजावण्यासाठी दाखवला आहे.

Figure 1: DataFrame anatomy: row labels, rows and column labels
DataFrame anatomy: row labels, rows and column labelsPython dataPython datapd.DataFrame()pd.DataFrame()Index (row label)012nameAshaRaviMeeraage212322cityPuneNashikMumbaiColumn labels are shownacross the topColumn labels are shown across the topEach horizontal recordis one row; the lefthelper column shows itsindex label.Each horizontal record is one row; the left helper column shows its index label.

या figure मधून तीन गोष्टी वेगळ्या करा:

Row#

Horizontal record म्हणजे row.

उदाहरणार्थ:

Asha | 21 | Pune

ही एका student ची row आहे.

Column#

Vertical field म्हणजे column.

उदाहरणार्थ age column मध्ये:

21, 23, 22

या values आहेत.

Index#

प्रत्येक row ला identify करणारा label म्हणजे index label.

Default:

0, 1, 2

Custom:

student_1, student_2, student_3

Remember

Row म्हणजे record.
Column म्हणजे field.
Index म्हणजे row label.

हे तीन concepts स्पष्ट झाले तर पुढचे Pandas operations खूप सोपे वाटतात.

11. DataFrame चा index तपासणे#

DataFrame च्या row labels पाहण्यासाठी:

PYTHON
df.index

वापरतो.

उदाहरण:

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "score": [75, 88, 92]
}

df = pd.DataFrame(
    data,
    index=["s1", "s2", "s3"]
)

print(df.index)

df.index आपल्याला row-label structure देते.

Beginner साठी values अधिक सहज पाहायच्या असतील तर:

PYTHON
print(list(df.index))

यातून conceptually:

['s1', 's2', 's3']

मिळेल.

इथे महत्त्वाचे काय?

index rows शी संबंधित आहे.

Columns शी नाही.

12. DataFrame चे columns तपासणे#

Column labels पाहण्यासाठी:

PYTHON
df.columns

उदाहरण:

PYTHON
data = {
    "name": ["Asha", "Ravi"],
    "age": [21, 23],
    "city": ["Pune", "Nashik"]
}

df = pd.DataFrame(data)

print(df.columns)

Beginner-friendly form मध्ये:

PYTHON
print(list(df.columns))

Conceptually:

['name', 'age', 'city']

म्हणजे DataFrame मध्ये कोणते fields उपलब्ध आहेत हे columns मधून समजू शकते.

Key Idea

df.index → row labels
df.columns → column labels

13. DataFrame चा shape तपासणे#

DataFrame किती मोठा आहे?

किती rows आहेत?

किती columns आहेत?

यासाठी shape वापरतो.

PYTHON
df.shape

समजा DataFrame मध्ये 3 rows आणि 3 columns आहेत.

मग:

df.shape

ची value:

(3, 3)

अशी असेल.

पहिली संख्या → rows

दुसरी संख्या → columns

म्हणजे:

shape = (rows, columns)

उदाहरण:

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "age": [21, 23, 22],
    "city": ["Pune", "Nashik", "Mumbai"]
}

df = pd.DataFrame(data)

print(df.shape)

इथे DataFrame मध्ये:

  • 3 rows
  • 3 columns

आहेत.

म्हणून shape 3 × 3 आहे.

Common Mistake

shape method नाही. तो attribute आहे. म्हणून df.shape लिहायचे, df.shape() नाही.

14. Total Elements तपासण्यासाठी size#

shape आपल्याला dimensions सांगतो.

पण एकूण किती data cells आहेत हे जाणून घ्यायचे असेल तर size वापरता येतो.

PYTHON
df.size

समजा:

df.shape → (3, 3)

तर:

3 rows × 3 columns = 9 elements

म्हणून:

df.size → 9

उदाहरण:

PYTHON
print(df.shape)
print(df.size)

Shape आणि size मधील फरक:

Attribute काय सांगतो? Example
shape rows आणि columns ची संख्या (3, 3)
size एकूण data elements 9
Interview Point

shape dimensions देते. size total element count देते.

15. Column Data Types तपासण्यासाठी dtypes#

DataFrame मध्ये प्रत्येक column मध्ये कोणत्या प्रकारचा data आहे हे समजणे खूप महत्त्वाचे आहे.

यासाठी:

PYTHON
df.dtypes

वापरतो.

उदाहरण:

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "age": [21, 23, 22],
    "passed": [True, True, False]
}

df = pd.DataFrame(data)

print(df.dtypes)

इथे logically:

  • name → text-like values
  • age → integer values
  • passed → boolean values

अशा वेगवेगळ्या प्रकारचे columns आहेत.

Exact displayed dtype names काही cases मध्ये Pandas version, configuration आणि input representation नुसार बदलू शकतात.

पण beginner म्हणून मुख्य concept हे आहे:

dtypes प्रत्येक column चा inferred/stored data type दाखवतो.

Common Mistake

df.dtype आणि df.dtypes गोंधळू नका. DataFrame मध्ये अनेक columns असल्यामुळे आपण df.dtypes वापरतो.

16. Heterogeneous Column Data Types म्हणजे काय?#

heterogeneous हा शब्द सुरुवातीला मोठा वाटतो.

पण idea अगदी सोपी आहे.

समजा आपला DataFrame असा आहे:

name age salary active
Asha 25 45000.5 True
Ravi 28 52000.0 False

इथे:

  • name → text
  • age → integer-like
  • salary → decimal numeric
  • active → boolean

सगळ्या columns मध्ये एकाच प्रकारचा data असण्याची गरज नाही.

यालाच आपण साध्या भाषेत heterogeneous column data types म्हणू शकतो.

म्हणजे:

एका DataFrame मधील वेगवेगळ्या columns चे data types वेगवेगळे असू शकतात.

हे DataFrame चे practical strength आहे.

Real-world business data मध्ये एका table मध्ये नाव, तारीख, quantity, price, status अशा अनेक प्रकारची माहिती एकत्र असते.

महत्त्वाचे

वेगवेगळ्या columns चे dtypes वेगवेगळे असणे normal आहे. पण एका single column मध्ये unrelated mixed values टाकणे analysis साठी अनेकदा problematic ठरू शकते.

17. DataFrame empty आहे का ते तपासणे#

DataFrame मध्ये structural data आहे की नाही हे पटकन तपासण्यासाठी:

PYTHON
df.empty

वापरतो.

हा boolean attribute आहे.

म्हणजे result:

True

किंवा:

False

असा असतो.

Empty DataFrame#

PYTHON
df = pd.DataFrame()

print(df.empty)

इथे df.empty → True

कारण DataFrame कडे rows आणि columns चा usable structure नाही.

Non-empty DataFrame#

PYTHON
df = pd.DataFrame({
    "name": ["Asha"]
})

print(df.empty)

इथे df.empty → False

कारण DataFrame मध्ये row आणि column आहेत.

Key Idea

empty हा structural check आहे. तो missing values किती आहेत हे मोजत नाही.

उदाहरणार्थ DataFrame मध्ये rows आणि columns अस्तित्वात असतील, पण values missing असतील, तरी तो फक्त त्या कारणामुळे automatically empty होत नाही.

Missing data आपण वेगळ्या topic मध्ये व्यवस्थित हाताळतो.

18. Basic DataFrame Structure कशी inspect करायची?#

Unknown DataFrame हातात आला की लगेच complex analysis सुरू करण्यापेक्षा आधी त्याची basic structure समजून घ्या.

"घाईचे काम शैतानाचे" हे data analysis मध्येही लागू पडते.

Basic inspection साठी आपण या chapter मधील attributes वापरू शकतो:

PYTHON
print(df.index)
print(df.columns)
print(df.shape)
print(df.size)
print(df.dtypes)
print(df.empty)

प्रत्येकाचा वेगळा purpose आहे.

Attribute मुख्य प्रश्न
index Rows ची labels कोणती आहेत?
columns Columns कोणते आहेत?
shape किती rows आणि columns आहेत?
size एकूण किती data elements आहेत?
dtypes प्रत्येक column चा data type काय आहे?
empty DataFrame structurally empty आहे का?

ही एक useful inspection sequence आहे:

Figure 2: Basic DataFrame structure inspection
Basic DataFrame structure inspectionCheck row andcolumn labelsCheck row and column labelsCheck shapeand total sizeCheck shape and total sizeCheck columndata typesCheck column data typesCheck whetherthe DataFrameis emptyCheck whether the DataFrame is empty

पहिल्यांदा labels समजून घ्या.

नंतर dimensions.

नंतर data types.

आणि मग basic structural state.

या चार गोष्टींमुळे DataFrame बद्दल सुरुवातीचा mental picture तयार होतो.

19. एक Complete Mini Example#

आता या chapter मधील मुख्य ideas एकत्र वापरू.

आपल्याकडे employees चा data आहे:

PYTHON
import pandas as pd

employees = [
    ["Asha", 25, 45000.50, True],
    ["Ravi", 28, 52000.00, False],
    ["Meera", 26, 48000.75, True]
]

df = pd.DataFrame(
    employees,
    columns=["name", "age", "salary", "active"],
    index=["emp_101", "emp_102", "emp_103"]
)

इथे आपण काय केले?

पहिले:

employees हा List of Lists आहे.

प्रत्येक inner list एक employee row represent करते.

नंतर:

PYTHON
columns=["name", "age", "salary", "active"]

देऊन प्रत्येक position ला meaning दिला.

त्यानंतर:

PYTHON
index=["emp_101", "emp_102", "emp_103"]

देऊन प्रत्येक employee row ला custom label दिला.

आता basic inspection:

PYTHON
print(list(df.index))
print(list(df.columns))
print(df.shape)
print(df.size)
print(df.dtypes)
print(df.empty)

यामधून आपण logically खालील गोष्टी समजू शकतो:

Inspection Meaning
index emp_101, emp_102, emp_103 हे row labels आहेत
columns name, age, salary, active हे fields आहेत
shape 3 rows आणि 4 columns
size 12 data elements
dtypes columns च्या stored data types ची माहिती
empty False, कारण DataFrame मध्ये data structure आहे

size का 12?

कारण:

3 rows × 4 columns = 12 cells

Index labels या calculation मध्ये data columns म्हणून count होत नाहीत.

हा point विशेष लक्षात ठेवा.

20. Common Mistakes#

Mistake 1 — Dictionary मधील lists ची length mismatch ठेवणे#

Problem:

PYTHON
data = {
    "name": ["Asha", "Ravi", "Meera"],
    "age": [21, 23]
}

एक column तीन rows सुचवतो.

दुसरा column दोन rows सुचवतो.

Normal rectangular DataFrame तयार करताना हे inconsistent आहे.

Mistake 2 — List of Lists वापरून column names न देणे#

PYTHON
data = [
    ["Asha", 21],
    ["Ravi", 23]
]

df = pd.DataFrame(data)

हे valid असू शकते.

पण columns ला default positional labels मिळतील.

Learner किंवा developer साठी name, age यांसारखी meaningful labels अधिक useful असतात.

Mistake 3 — index ला normal column समजणे#

Custom index:

PYTHON
index=["s1", "s2"]

हा row labeling mechanism आहे.

तो automatically regular data column होत नाही.

Mistake 4 — shape आणि size सारखेच आहेत असे समजणे#

shape → dimensions

size → total elements

उदाहरण:

shape = (5, 3)

तर:

size = 15

Mistake 5 — dtypes म्हणजे पूर्ण DataFrame चा एकच data type असे समजणे#

DataFrame मध्ये प्रत्येक column चा dtype वेगळा असू शकतो.

म्हणून dtypes plural आहे.

Mistake 6 — empty म्हणजे missing values आहेत असे समजणे#

df.empty missing-value detector नाही.

तो DataFrame ची structural emptiness तपासतो.

21. Interview Preparation#

What is a pandas DataFrame?Show Answer

A pandas DataFrame is a two-dimensional labelled tabular data structure organized into rows and columns. It supports row and column labels and allows different columns to have different data types.

Key Points:

  • Two-dimensional structure
  • Labelled rows and columns
  • Suitable for tabular data
  • Different columns can have different dtypes

Why interviewer asks this:

The interviewer wants to verify that you understand the central data structure used throughout pandas.


What is the difference between DataFrame shape and size?Show Answer

DataFrame.shape returns a tuple containing the number of rows and columns, while DataFrame.size returns the total number of data elements in the DataFrame.

Key Points:

  • shape returns (rows, columns)
  • size returns total elements
  • For a normal rectangular DataFrame, size is rows multiplied by columns

Why interviewer asks this:

The interviewer checks whether you can correctly inspect DataFrame dimensions and avoid confusing related attributes.


What is the difference between index and columns in a DataFrame?Show Answer

The index represents row labels, while columns represents column labels. Both provide labels for different axes of the DataFrame.

Key Points:

  • index identifies rows
  • columns identifies fields or columns
  • Custom index labels can be assigned during DataFrame construction

Why interviewer asks this:

Correct understanding of DataFrame axes is essential for later data selection, alignment, and analysis operations.


Can different columns in a pandas DataFrame have different data types?Show Answer

Yes. A DataFrame can contain columns with different data types. For example, one column may contain numeric values, another text values, and another boolean values.

Key Points:

  • DataFrame supports heterogeneous column dtypes
  • df.dtypes reports the dtype of each column
  • This is useful for real-world tabular datasets

Why interviewer asks this:

The interviewer wants to confirm that you understand how DataFrames differ from purely homogeneous tabular structures.

22. या Chapter मधून नेमके काय लक्षात ठेवायचे?#

  • pd.DataFrame() हा DataFrame तयार करण्याचा मुख्य constructor आहे.
  • Dictionary of Lists मध्ये प्रत्येक key साधारणपणे एक column बनते.
  • List of Dictionaries मध्ये प्रत्येक dictionary साधारणपणे एक row बनते.
  • List of Lists मध्ये प्रत्येक inner list एक row बनू शकते.
  • NumPy Array मधील values DataFrame मध्ये आणून meaningful column labels देता येतात.
  • columns parameter column labels देतो.
  • index parameter row labels देतो.
  • df.index row labels दाखवतो.
  • df.columns column labels दाखवतो.
  • df.shape (rows, columns) सांगतो.
  • df.size total data elements सांगतो.
  • df.dtypes प्रत्येक column चा data type सांगतो.
  • DataFrame च्या वेगवेगळ्या columns चे dtypes वेगवेगळे असू शकतात.
  • df.empty हा structural emptiness check आहे.
  • Unknown DataFrame वर काम सुरू करण्यापूर्वी labels, shape, size, dtypes आणि empty state समजून घेणे ही चांगली habit आहे.
Interactive learning

Practice lab

Apply this chapter with guided activities, immediate feedback, explanations, and retry.

Practice loads when this section enters view.