PandasCore· 35 min read

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:

The sample table for this 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:

Keep only rows where amount is over 500
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.

Two conditions joined with & (and)
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.

GoalCode
One columndf['amount']
Several columnsdf[['product', 'amount']]
Rows by conditiondf[df['amount'] > 500]
ANDdf[(a) & (b)]
ORdf[(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”?

Answer: df[df['region'] == 'North'] builds a True/False mask from the condition and keeps the matching rows.

✍️ Practice

  1. From the sample table, select just the product and amount columns.
  2. Filter the table to rows where region is “South” OR amount is over 800.

🏠 Homework

  1. 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.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →