Python lesson
Filtering with |
Every Earbuds order and every Charger order are kept.
Try this lesson free →Example
Join two tests with | (the bar key) when either one is enough. Each test still goes in its own brackets:
df[(df["Product"] == "Earbuds") | (df["Product"] == "Charger")]Order Day Store Product Qty Revenue
0 O101 Mon Lagos Earbuds 2 70
1 O102 Mon Abuja Charger 1 15
3 O104 Wed Lagos Earbuds 1 35
4 O105 Wed Abuja Earbuds 4 140
5 O106 Thu Lagos Charger 2 30
7 O108 Fri Lagos Earbuds 3 105Key points
(test1) | (test2)Either one is enough. Like OR in a spreadsheet.
BracketsWrap each test in its own ( ), just like with &.
| not orIn pandas, use |. The word or doesn't work on whole columns.
Put together: df[(df["Product"] == "Earbuds") | (df["Product"] == "Charger")]. & needs both; | needs one.
Practice
1. Build a line that keeps orders for Earbuds or Charger.
df[(df["Product"] == "Earbuds") | (df["Product"] == "Charger")]2. Build a line that keeps orders from Monday or Friday.
df[(df["Day"] == "Mon") | (df["Day"] == "Fri")]3. Show the orders from Tuesday or Thursday.
df[(df["Day"] == "Tue") | (df["Day"] == "Thu")]4. Show the orders that are from Abuja or worth more than 100.
df[(df["Store"] == "Abuja") | (df["Revenue"] > 100)]5. Show the Phone case or Charger orders.
df[(df["Product"] == "Phone case") | (df["Product"] == "Charger")]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 →