Informatics Practices · Class 12 · Python Pandas · DataFrame
PandasDataFrame⏱️ 8 min read
Boolean Indexing
Boolean indexing is how you ask a DataFrame a question and keep only the rows that answer yes. It's the heart of real data analysis — "show me everyone who…".
1Filter by a condition
A comparison like df['IP Marks'] > 85 produces a True/False for every row. Put that back inside df[...] and only the True rows survive. Slide the threshold:
Boolean filtering
A condition turns into a True/False test for every row. Feed it back into df[...] and only the True rows survive.
df[df['IP Marks'] > 85]
Name | English | IP | Maths | |
|---|---|---|---|---|
ID 1 | Rinku | 65 | 89 | 72 |
ID 2 | Ritu | 77 | 83 | 69 |
ID 3 | Pankaj | 72 | 95 | 87 |
ID 4 | Ajay | 81 | 99 | 94 |
ID 5 | Aditya | 92 | 87 | 96 |
TrueFalseTrueTrueTrue
4 of 5 rows have IP Marks above 85. Only those come back — the True ones.
2One condition
df_filter.py
3Combining conditions with & and |
Join conditions with & (and) or | (or). Each condition must be wrapped in its own parentheses — this trips up almost everyone.
df_filter_combined.py
Watch Out
Use
& and |, not the Python words and/or — and wrap every condition in parentheses. df[df.a > 1 & df.b < 2] without brackets is a common bug.Key Takeaway
Boolean indexing = build a True/False mask from a condition, then
df[mask] keeps the True rows. Combine masks with &/| and parenthesise each part. Quick Check
What does df[df['Maths Marks'] > 90] return?
Quick Check
Which correctly filters rows where a > 1 AND b < 5?