1. Introduction: Accessing and Modifying DataFrame Subsets
Once a dataset is loaded into a DataFrame, data scientists rarely process the entire table at once. Machine learning preprocessing, exploratory analysis, and data cleaning require accessing, filtering, and modifying specific subsets of data.
Pandas provides two primary methods for subset selection:
- loc (Location) — label-based selection
- iloc (Index Location) — integer position-based selection
df.loc[row_labels, col_labels] — label-based; slicing includes both start and end boundaries
df.iloc[row_positions, col_positions] — integer positions; slicing excludes the end boundary
- Boolean indexing:
df[df['Column'] > value]
- Conditional assignment:
df.loc[condition, col] = value
2. Environment Setup: Creating the Sample DataFrame
Import Pandas as pd and build a DataFrame with four features (Name, Age, City, Salary) and four records:
import pandas as pd
# Creating a DataFrame with 4 features and 4 records
data = {
"Name": ["Alice", "Bob", "Charlie", "David"],
"Age": [25, 35, 28, 42],
"City": ["New York", "London", "Paris", "Tokyo"],
"Salary": [70000, 85000, 62000, 95000]
}
df = pd.DataFrame(data)
print(df)
Output structure (default row indices 0–3):
Name Age City Salary
0 Alice 25 New York 70000
1 Bob 35 London 85000
2 Charlie 28 Paris 62000
3 David 42 Tokyo 95000
3. Label-Based Selection: df.loc
The df.loc indexer selects data using row and column labels.
Syntax: df.loc[row_selection, column_selection].
# Selecting rows with labels 1 to 3, and columns 'Name' and 'Salary'
subset_loc = df.loc[1:3, ["Name", "Salary"]]
print(subset_loc)
Name Salary
1 Bob 85000
2 Charlie 62000
3 David 95000
- Row labels here match default indices (0, 1, 2, 3).
- Columns are selected by name:
"Name" and "Salary".
- Critical rule for loc: both endpoints are included —
1:3 returns labels 1, 2, and 3 (three rows).
4. Position-Based Selection: df.iloc
The df.iloc (Integer Location) indexer selects strictly by zero-based integer positions for rows and columns.
Syntax: df.iloc[row_positions, column_positions].
# Selecting row indices 0:2 and column indices 1:3
subset_iloc = df.iloc[0:2, 1:3]
print(subset_iloc)
Age City
0 25 New York
1 35 London
- Rows
0:2 → indices 0 and 1 (stop 2 excluded).
- Columns
1:3 → Age and City (column 3 Salary excluded).
5. Key Difference: Boundary Inclusivity (loc vs. iloc)
| Indexer |
Based On |
Start |
End |
Example (1:3) |
| df.loc |
Labels / names |
Included |
Included |
Labels 1, 2, 3 (3 records) |
| df.iloc |
Integer positions |
Included |
Excluded |
Positions 1 and 2 (2 records) |
In iloc, the endpoint is excluded (standard Python slicing). In loc, both endpoints are included because labels are identifiers, not offsets.
6. Boolean Indexing: Filtering Rows with Conditions
# Filtering records where Salary is greater than 75,000
high_salary = df[df["Salary"] > 75000]
print(high_salary)
Name Age City Salary
1 Bob 35 London 85000
3 David 42 Tokyo 95000
Only Bob (85,000) and David (95,000) satisfy the condition.
7. Modifying Data Subsets Using df.loc
Using column labels with loc is preferred over integer positions: large tables make counting columns error-prone, and reordering or adding columns would break positional code.
Example — set salary to 65,000 for employees under age 30:
# Targeting rows where Age < 30 and updating the 'Salary' column
df.loc[df["Age"] < 30, "Salary"] = 65000
# Inspecting records where Age < 30 to verify the change
print(df[df["Age"] < 30])
Name Age City Salary
0 Alice 25 New York 65000
2 Charlie 28 Paris 65000
Alice and Charlie are updated; the filtered print confirms both now show 65000.