Selecting Columns & Filtering Rows
Zoom in on the data you care about — pick the columns you need and keep only the rows that match a condition.
What you will learn
- Select one or more columns
- Filter rows with a condition
- Combine conditions with & and |
Two ways to zoom in
A real table is huge. You rarely want all of it. There are two moves you will use constantly: picking columns (the fields you care about) and filtering rows (only the records that match a rule).
We will use this small sales table throughout the lesson:
import pandas as pd
df = pd.DataFrame({
'product': ['Keyboard', 'Mouse', 'Monitor', 'Mouse', 'Monitor'],
'region': ['North', 'South', 'East', 'North', 'South'],
'amount': [250, 120, 900, 130, 850]
})Note: Output: (No output — this just builds the table. Five rows: products, their region, and the sale amount.)
Filter rows with a condition
To keep only some rows, put a condition inside the brackets. Pandas keeps every row where the condition is True. Here we keep only the big sales:
big = df[df['amount'] > 500]
print(big)Note: Output: product region amount 2 Monitor East 900 4 Monitor South 850 Only the two sales above 500 survived. The condition df['amount'] > 500 is a True/False mask; Pandas keeps the True rows.
Combine conditions
Use & for and, | for or. Every condition must be wrapped in its own ( ). Here: monitors that also sold for more than 870.
result = df[(df['product'] == 'Monitor') & (df['amount'] > 870)]
print(result)Note: Output: product region amount 2 Monitor East 900 Only one row is both a Monitor AND above 870. The 850 monitor failed the second test, so it was dropped.
| Goal | Code |
|---|---|
| One column | df['amount'] |
| Several columns | df[['product', 'amount']] |
| Rows by condition | df[df['amount'] > 500] |
| AND | df[(a) & (b)] |
| OR | df[(a) | (b)] |
Watch out: In Pandas conditions, use & and | — not the words and / or — and wrap each condition in brackets. Forgetting the brackets is the number-one beginner error here.
Tip: Read df[df['amount'] > 500] from the inside out: first df['amount'] > 500 makes a True/False list, then df[ ... ] keeps the True rows. Once you see the two steps, filtering clicks.
Q. Which line keeps only rows where region is “North”?
✍️ Practice
- From the sample table, select just the
productandamountcolumns. - Filter the table to rows where
regionis “South” ORamountis over 800.
🏠 Homework
- Load a CSV and write two filters: one with a single condition, and one that combines two conditions with
&or|. Print how many rows each returns.