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:
- Select columns:
df[["city", "sales"]] - Mask:
mask = df["channel"] == "Online" - Filter:
online = df[mask].copy() - Sort:
online.sort_values("sales", ascending=False) - Check:
online["sales"].sum()→ 190 on the sample. - Combine conditions with
&/|and parentheses.
select → mask → filter → sort → verify sum
Language: English · मराठी · हिंदी
What do I need before this guide?
- A DataFrame already loaded.
- Course: Selecting columns and rows.
Before and after (look at the tables first)
Before — full shop table; all rows total 260.
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:
- Use
&and|, not Englishand/or, between Series. - Wrap each test in parentheses.
- 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
- Row count drops after a filter.
- Index may look “gappy” (0, 2…) — normal;
reset_index(drop=True)if you want clean positions. - 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:
- Filter Store rows; sum should be 70.
- Filter Online; sum 190.
- Sort Online descending; first city Mumbai.
- 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.