2. DataFrame Operations, Indexing & Slicing
CBSE Syllabus Alignment • Subject Code 065
- Creation of DataFrame from: dictionary of Series, list of dictionaries, 2D ndarrays.
- Operations on rows & columns: add, select, delete, rename.
- Head and Tail functions, Indexing using labels (
loc), positional indexing (iloc), boolean indexing.- Iterating through DataFrame rows (
iterrows) and columns (items).
1. What is a Pandas DataFrame?
A Pandas DataFrame is a two-dimensional, size-mutable, and value-mutable tabular data structure with labeled axes (rows and columns). It is similar to a spreadsheet, SQL table, or dictionary of Series objects.
Structural Anatomy:
Columns Axis (axis=1 / df.columns)
Col 0 Col 1 Col 2
┌────────────┬────────────┬───────────┐
Row 0 │ 'Aanya' │ 98 │ 'A1' │
Index Axis ├────────────┼────────────┼───────────┤
(axis=0 / │ 'Rohan' │ 85 │ 'A2' │
df.index) ├────────────┼────────────┼───────────┤
Row 2 │ 'Pooja' │ 92 │ 'A1' │
└────────────┴────────────┴───────────┘| Property | Series (1D) | DataFrame (2D) |
|---|---|---|
| Dimensions | 1 Dimension (shape = (n,)) | 2 Dimensions (shape = (rows, cols)) |
| Data Homogeneity | Strictly Homogeneous (1 dtype) | Heterogeneous (each column can have its own dtype) |
| Size Mutability | Size Immutable | Size Mutable (columns/rows can be added or removed) |
| Value Mutability | Value Mutable | Value Mutable |
2. DataFrame Creation Methods
Method A: From a Dictionary of Lists / Arrays
Each dictionary key becomes a column name, and each dictionary list forms the data of that column.
python
import pandas as pd
data = {
'Student': ['Aanya', 'Rohan', 'Pooja', 'Kabir'],
'IP_Score': [98, 85, 92, 78],
'Attendance': [95.5, 88.0, 92.5, 81.0]
}
df = pd.DataFrame(data, index=['R101', 'R102', 'R103', 'R104'])
print(df)Output:
Student IP_Score Attendance
R101 Aanya 98 95.5
R102 Rohan 85 88.0
R103 Pooja 92 92.5
R104 Kabir 78 81.0WARNING
When creating a DataFrame from a dictionary of lists, all lists must have the exact same length, otherwise a ValueError: All arrays must be of the same length will occur.
Method B: From a List of Dictionaries
Each dictionary in the list represents a single row. Dictionary keys become column headers. Missing keys in any dictionary automatically become NaN.
python
students_list = [
{'Name': 'Aanya', 'Score': 98, 'City': 'Delhi'},
{'Name': 'Rohan', 'Score': 85}, # 'City' missing -> NaN
{'Name': 'Pooja', 'Score': 92, 'City': 'Mumbai', 'Grade': 'A1'} # Extra 'Grade' key
]
df_from_list = pd.DataFrame(students_list, index=['S1', 'S2', 'S3'])
print(df_from_list)Output:
Name Score City Grade
S1 Aanya 98 Delhi NaN
S2 Rohan 85 NaN NaN
S3 Pooja 92 Mumbai A1Method C: From a Dictionary of Series
When creating from a dictionary of Series, Pandas automatically aligns the index labels of the individual Series. Non-matching indices receive NaN.
python
s_math = pd.Series([90, 80, 70], index=['Aanya', 'Rohan', 'Pooja'])
s_ip = pd.Series([98, 85, 95], index=['Aanya', 'Rohan', 'Zaid'])
df_series = pd.DataFrame({'Maths': s_math, 'IP': s_ip})
print(df_series)Output:
Maths IP
Aanya 90.0 98.0
Pooja 70.0 NaN
Rohan 80.0 85.0
Zaid NaN 95.03. Core DataFrame Attributes
| Attribute | Description | Example Output on df |
|---|---|---|
df.index | Row labels (index) | Index(['R101', 'R102', 'R103', 'R104'], dtype='object') |
df.columns | Column header labels | Index(['Student', 'IP_Score', 'Attendance'], dtype='object') |
df.dtypes | Data types of each column | Student: object, IP_Score: int64, Attendance: float64 |
df.shape | Tuple representing (num_rows, num_columns) | (4, 3) |
df.size | Total number of cells (rows * cols) | 12 |
df.ndim | Number of dimensions (always 2 for DataFrame) | 2 |
df.T | Transposes rows into columns and vice-versa | DataFrame of shape (3, 4) |
df.empty | Returns True if DataFrame has 0 rows | False |
4. Column & Row Operations
1. Selecting Columns
- Single Column (returns 1D Series):
df['Student']ordf.Student - Multiple Columns (returns 2D DataFrame):
df[['Student', 'IP_Score']]
2. Adding a New Column
python
# Adding by direct scalar or computation:
df['Grade'] = ['A1', 'A2', 'A1', 'B1']
df['Bonus_Score'] = df['IP_Score'] + 23. Deleting Columns
delstatement:del df['Bonus_Score'](modifies inplace)pop()method:removed_col = df.pop('Grade')(deletes and returns the Series)drop()method:df.drop(['Attendance'], axis=1, inplace=True)
IMPORTANT
Understanding axis in Pandas:
axis=0oraxis='index': Operates along Rows (default in drop).axis=1oraxis='columns': Operates along Columns.
4. Adding and Deleting Rows
- Add Row via
loc:df.loc['R105'] = ['Dev', 89, 90.0] - Delete Row via
drop:df.drop(['R104'], axis=0, inplace=True)
5. Renaming Columns and Index
python
df.rename(
columns={'IP_Score': 'Informatics_Marks', 'Student': 'Candidate_Name'},
index={'R101': 'ID_101'},
inplace=True
)5. Selection & Slicing: loc vs iloc
The choice between loc and iloc is the most frequently tested concept in CBSE Class 12 IP board exams.
mermaid
graph TD
subgraph loc - Label Based
L1["df.loc[row_label, col_label]"]
L2["Explicit Index Names"]
L3["Endpoint INCLUDED in slice!"]
end
subgraph iloc - Positional Based
I1["df.iloc[row_pos, col_pos]"]
I2["0-based Integer Offsets"]
I3["Endpoint EXCLUDED in slice!"]
endComparative Slicing Matrix:
Given sample DataFrame df:
Student IP_Score Attendance
R101 Aanya 98 95.5
R102 Rohan 85 88.0
R103 Pooja 92 92.5
R104 Kabir 78 81.0Example 1: Selecting a single value
python
# Extract Pooja's IP_Score (row R103, col 'IP_Score'):
val1 = df.loc['R103', 'IP_Score'] # 92
val2 = df.iloc[2, 1] # 92 (row position 2, col position 1)Example 2: Slicing multiple rows and columns
python
# Using loc (BOTH endpoints R103 and 'IP_Score' are INCLUDED):
sub_loc = df.loc['R101':'R103', 'Student':'IP_Score']
print(sub_loc)
# Student IP_Score
# R101 Aanya 98
# R102 Rohan 85
# R103 Pooja 92
# Using iloc (Stop positions 3 and 2 are EXCLUDED):
sub_iloc = df.iloc[0:3, 0:2]
print(sub_iloc)
# Same tabular output!Boolean Filtering:
python
# Filter rows where IP_Score >= 90:
top_performers = df[df['IP_Score'] >= 90]
# Multiple conditions using bitwise operators (& for AND, | for OR, ~ for NOT):
result = df[(df['IP_Score'] >= 90) & (df['Attendance'] > 90.0)]6. Iterating Over a DataFrame
1. iterrows(): Iterating Row-by-Row
Yields pairs of (row_index_label, row_Series):
python
for row_label, row_series in df.iterrows():
print(f"Index: {row_label}")
print(f"Student: {row_series['Student']}, Score: {row_series['IP_Score']}")2. items() / iteritems(): Iterating Column-by-Column
Yields pairs of (column_name, column_Series):
python
for col_name, col_series in df.items():
print(f"Column: {col_name}, Data Type: {col_series.dtype}")Interactive Lab & Board Practice
Multi-Mode DiagramPandas DataFrame: 2D Heterogeneous Tabular Data Structure & loc/iloc Slicing
Option 1: Publication-Grade Scientific Vector SVG
Two-dimensional tabular data structure with labeled axes (index rows and columns). Illustrating explicit label indexing (loc[row_label, col_label]) vs zero-based integer position indexing (iloc[row_pos, col_pos]).
loc (Label-based Slicing, Both Ends Included):
iloc (Position-based Slicing, End Excluded):
📇 Active Recall Information Practices Deck
DataFrame loc & iloc Mastery Deck
Card 1 / 5
Unit 1: Pandas DataFrames👆 Click card to reveal equation & mechanism
df.loc vs df.iloc Indexing Behavior
Given DataFrame `df` with custom index `['A', 'B', 'C']`, contrast `df.loc['A':'B']` with `df.iloc[0:2]`.
Recall the equation, boundary limits, and traps before flipping!
✓ Verified Chemistry Formula👆 Click to flip back
📐 Condition / DomainBoth return a DataFrame subset. `df.loc` matches explicit row index labels; `df.iloc` uses zero-based positional integers.
🔍 Boundary Check`df.loc['A':'B']` returns rows A and B. `df.iloc[0:2]` returns rows at positions 0 and 1 (A and B).
⚠️ Common Exam Trap:For integer indices like [1, 2, 3], `df.loc[1:2]` includes row 2, while `df.iloc[1:2]` returns ONLY row at index position 1!
🛡️ Negative-Marking & Output Defense
Spot the Syntax & Output Bug: CBSE IP Code Debugger
Identify the exact line where an illegal syntax, invalid SQL clause, or incorrect index assumption was introduced.
CBSE Problem Statement
Write a query to display the Department and Maximum Salary for departments where the Average Salary exceeds 60,000.
Step-by-Step Code / Output: Click the step that contains the error
Line 1
SELECT Department, MAX(Salary) FROM EmployeeClick to test
Line 2
WHERE AVG(Salary) > 60000Click to test
Line 3
GROUP BY Department;Click to test