Filter Rows
A boolean condition gives a mask. Put the mask inside brackets to keep the matching rows.
Example
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['Ada', 'Bob', 'Cy', 'Di'], 'age': [30, 25, 35, 28], 'city': ['Lagos', 'Accra', 'Lagos', 'Nairobi'], 'score': [88.5, 92.0, 79.5, 85.0]})
mask = df['age'] > 27
print(mask.tolist())
print(df[mask][['name', 'age']])
print(df[df['city'] == 'Lagos']['name'].tolist())Result
[True, False, True, True] name age 0 Ada 30 2 Cy 35 3 Di 28 ['Ada', 'Cy']
Exercise
Rows with score above 80.
df[df["score"] 80]
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.