Ravindra BagaleCourses & study guides Track your progress

Guides

How to Filter Rows and Columns in pandas

Read this guide in: English · मराठी · हिंदी

Select columns, build a boolean mask, filter rows, then sort. Example: keep Online shop rows — Mumbai 120 + Pune 70 = 190.

Chala mitrano! Full tables hide the story. Filters reveal it. Today select, boolean filter and sort with shop sales. Vertical steps only. Samajla ka?

Quick answer

Vertical filter path:

  1. Select columns: df[["city", "sales"]]
  2. Mask: mask = df["channel"] == "Online"
  3. Filter: online = df[mask].copy()
  4. Sort: online.sort_values("sales", ascending=False)
  5. Check: online["sales"].sum() → 190 on the sample.
  6. Combine conditions with & / | and parentheses.
select → mask → filter → sort → verify sum

Language: English · मराठी · हिंदी

What do I need before this guide?

Before and after (look at the tables first)

Before filter Four shop rows; total 260.

Before — full shop table; all rows total 260.

After filter Online rows sorted; total 190.

After — Online filter + sort; total 190.

Before: all rows total 260. After: Online only totals 190.

Sample shop table

city channel sales
Mumbai Online 120
Mumbai Store 40
Pune Online 70
Nashik Store 30

All-rows sum = 260. Online sum = 190.

Select columns

df[["city", "channel", "sales"]]
df["sales"]                 # Series

Why you care: plotting the wrong columns wastes time; select first.

Boolean filters

mask = df["channel"] == "Online"
online = df[mask].copy()
online["sales"].sum()  # 190

Course: Boolean filters.

Combine conditions

df[(df["channel"] == "Online") & (df["sales"] >= 70)]
df[df["city"].isin(["Mumbai", "Pune"])]

Rules:

  1. Use & and |, not English and / or, between Series.
  2. Wrap each test in parentheses.
  3. Prefer .copy() if you will edit the result.

Sorting and top N

online.sort_values("sales", ascending=False)
online.nlargest(1, "sales")  # Mumbai 120

Course: Sorting and top N.

Instagram-like example

Filter posts with likes >= 70, then sort descending. Managers see winners first.

What you see

  1. Row count drops after a filter.
  2. Index may look “gappy” (0, 2…) — normal; reset_index(drop=True) if you want clean positions.
  3. Sums change — that is the point.

Ravindra Bagale's Tip

Say the filter in Marathi-English out loud: “Online only, then sort sales.” If you cannot say it, do not code it yet. Samajla ka?

Practice task

Using the four-row shop table:

  1. Filter Store rows; sum should be 70.
  2. Filter Online; sum 190.
  3. Sort Online descending; first city Mumbai.
  4. Keep rows where city is Mumbai or Pune.

Samajla ka? Select columns, boolean mask, filter, sort, verify 190. Aata pudhe jaauya.

Frequently asked questions

loc vs just brackets?

Brackets are fine for simple column picks and masks. loc is clearer for label-based row+column work — both appear in the course.

Why .copy()?

Avoid SettingWithCopy surprises when you edit a filtered slice later.

How do I get top N?

sort_values then head(N), or nlargest on a column.

Filter on numbers?

df[df["sales"] >= 70] works the same way as text filters.

Multiple cities?

df[df["city"].isin(["Mumbai", "Pune"])]

Course links?

Selecting, boolean filters, sorting lessons in chapter 6.