Filtering Data
🎧 Focus sounds
Soft beat sounds that many learners like while reading. Pick one, press Play, and keep studying.
Keep the volume low. These sounds are for comfort while studying, not a medical treatment. If you have epilepsy or are sensitive to pulsing sounds, use the background noise only.
In this lesson
“Show me the students who scored above 70.” “Only orders from Pune.” Filtering is the question you’ll ask most often, and Pandas answers it with the same mask idea you learned in NumPy.
How a filter works#
A condition on a column gives a True/False mask, one value per row. Putting the mask in brackets keeps the True rows. Step through it:
import pandas as pd
df = pd.DataFrame({
"name": ["Asha", "Rahul", "Meera", "Kabir", "Zoya", "Dev"],
"city": ["Pune", "Surat", "Delhi", "Pune", "Delhi", "Surat"],
"marks": [82, 64, 91, 58, 77, 88],
})
mask = df["marks"] > 70
print(mask.tolist())
print(df[mask])
Output:
[True, False, True, False, True, True]
name city marks
0 Asha Pune 82
2 Meera Delhi 91
4 Zoya Delhi 77
5 Dev Surat 88
Usually you write it in one line: df[df["marks"] > 70].
Combining conditions#
Use & for “and”, | for “or”, ~ for “not”, and wrap each condition in brackets:
print(df[(df["marks"] > 70) & (df["city"] == "Delhi")])
print(df[(df["city"] == "Pune") | (df["marks"] >= 90)])
Output:
name city marks
2 Meera Delhi 91
4 Zoya Delhi 77
name city marks
0 Asha Pune 82
2 Meera Delhi 91
3 Kabir Pune 58
isin and between#
print(df[df["city"].isin(["Pune", "Surat"])]["name"].tolist())
print(df[df["marks"].between(60, 80)]["name"].tolist())
Output:
['Asha', 'Rahul', 'Kabir', 'Dev']
['Rahul', 'Zoya']
isin([...]): the value is one of these.between(a, b): from a to b, both included.
Text filters#
print(df[df["name"].str.startswith("A")]["name"].tolist())
print(df[df["name"].str.contains("e", case=False)]["name"].tolist())
Output:
['Asha']
['Meera', 'Dev']
query: filters that read like English#
print(df.query("marks > 70 and city != 'Delhi'"))
Output:
name city marks
0 Asha Pune 82
5 Dev Surat 88
Inside query you can use plain and, or, not. It’s easier to read for long conditions.
Counting matches#
print((df["marks"] > 70).sum())
print(len(df[df["city"] == "Pune"]))
Output:
4
2
Common mistake
df[df["marks"] > 70 & df["city"] == "Delhi"] without brackets gives a confusing error, because & is evaluated before >. Always bracket each condition.
Practice#
- Find the students from Surat with marks below 80.
- Find everyone whose name ends with “a”.
- How many students scored 60 or more?
Summary#
df[condition]keeps rows where the condition is True.- Combine with
&,|,~and brackets. isin,between,.str.containsandquerycover most real filters.
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.