Intermediate
Data Selection, Filtering, and Sorting in Pandas
📂 Phase 5: NumPy, Pandas, Data Processing Basics (Days 21–25) · Python ProgrammingOnce data is loaded into a DataFrame, the next essential skill is being able to select specific rows and columns, filter down to only the data you care about, and sort it into a useful order — the bread-and-butter operations behind almost every real data analysis task.
Sample DataFrame Used Throughout This Lesson
import pandas as pd
data = {
"name": ["Vishwas", "Priya", "Arjun", "Sneha"],
"age": [22, 21, 23, 24],
"city": ["Bengaluru", "Mumbai", "Chennai", "Bengaluru"],
"score": [88, 92, 75, 95]
}
df = pd.DataFrame(data)
.loc[] — Selecting by Label
print(df.loc[0]) # the entire row at index label 0
print(df.loc[0, "name"]) # "Vishwas" — a single cell, by row label + column name
print(df.loc[0:2, ["name", "score"]]) # rows 0-2, only the name and score columns
.iloc[] — Selecting by Integer Position
print(df.iloc[0]) # the first row, by POSITION (not label)
print(df.iloc[0, 1]) # row 0, column 1 (age) — purely positional
print(df.iloc[0:2]) # the first two rows
| Accessor | Selects By |
|---|---|
| .loc[] | Label (row/column NAME) |
| .iloc[] | Integer position (like a list index) |
Filtering Rows With a Condition
high_scorers = df[df["score"] > 85]
print(high_scorers)
# name age city score
# 0 Vishwas 22 Bengaluru 88
# 1 Priya 21 Mumbai 92
# 3 Sneha 24 Bengaluru 95
Combining Multiple Conditions
# Use & (and) / | (or) — NOT the plain Python "and"/"or" keywords
result = df[(df["score"] > 80) & (df["city"] == "Bengaluru")]
print(result)
# name age city score
# 0 Vishwas 22 Bengaluru 88
# 3 Sneha 24 Bengaluru 95
This is a very common beginner error: writing df["score"] > 80 and df["city"] == "Bengaluru" with the plain Python and raises a ValueError, because pandas needs the element-wise & operator (with each condition wrapped in parentheses) to compare every row correctly.
Filtering With .isin()
selected_cities = df[df["city"].isin(["Mumbai", "Chennai"])]
print(selected_cities)
Sorting a DataFrame
sorted_by_score = df.sort_values(by="score")
print(sorted_by_score) # ascending by default
sorted_desc = df.sort_values(by="score", ascending=False)
print(sorted_desc) # highest score first
# Sort by multiple columns
sorted_multi = df.sort_values(by=["city", "score"], ascending=[True, False])
Adding a New Column
df["grade"] = ["B", "A", "C", "A"]
print(df)
# Or computed from an existing column
df["bonus_score"] = df["score"] + 5
Grouping and Aggregating Data
city_avg = df.groupby("city")["score"].mean()
print(city_avg)
# city
# Bengaluru 91.5
# Chennai 75.0
# Mumbai 92.0
# Name: score, dtype: float64
groupby() splits the data into groups based on a column's values, then applies an aggregate function (here, .mean()) to each group separately — directly analogous to a SQL GROUP BY clause.
Interview tip: "What is the difference between .loc[] and .iloc[]?" is one of the most reliably asked pandas questions — the cleanest way to answer is: .loc uses LABELS (so .loc[0:2] is inclusive of both endpoints), while .iloc uses POSITIONS (so .iloc[0:2] excludes the endpoint, exactly like standard Python slicing).