BBampaa

Lessons › Python

Python lesson

Filtering, one condition

The test gives True or False for every row, and df[...] keeps the True ones. It's a spreadsheet filter, written in code.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

A test inside square brackets keeps only the matching rows:

df[df["Store"] == "Lagos"]

Output:

Order Day Store Product Qty Revenue
0 O101 Mon Lagos Earbuds 2 70
2 O103 Tue Lagos Phone case 3 24
3 O104 Wed Lagos Earbuds 1 35
5 O106 Thu Lagos Charger 2 30
7 O108 Fri Lagos Earbuds 3 105

Key points

df["Store"] == "Lagos"A test on every row: True or False.
df[ test ]Keep only the rows where the test is True.
df["Revenue"] > 100Numbers work too: > < == and !=

Put together: df[df["Revenue"] > 100] keeps the big orders.

Practice

Try each one in your head first, then open the answer.

1. Build a line that keeps only the Abuja orders.
df[df["Store"] == "Abuja"]
2. Build a line that keeps only orders worth more than 100.
df[df["Revenue"] > 100]
3. Show only the Earbuds orders.
df[df["Product"] == "Earbuds"]
4. Show the orders worth more than 50.
df[df["Revenue"] > 50]
5. Show the orders with a quantity less than 3.
df[df["Qty"] < 3]

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed