Python

Filtering, Grouping & Aggregation

Unlock the power of 'Group By'. Learn to filter complex datasets and perform powerful aggregations like SQL.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Unlock the power of 'Group By'. Learn to filter complex datasets and perform powerful aggregations like SQL. This hands-on tutorial focuses on practical implementation of filtering, grouping & aggregation concepts.

Module 6: Filtering, Grouping & Aggregation

This is where Pandas shines. You can effortlessly slice millions of rows based on complex conditions and summarize them with groupby—just like SQL but easier.


Lesson 13: Conditional Filtering

Filtering rows based on values.

Boolean Selection

# Select rows where Age > 25
adults = df[df['Age'] > 25]

# Multiple conditions (Use parentheses & bitwise operators!)
# AND: &, OR: |
target = df[(df['Age'] > 25) & (df['City'] == 'Paris')]

The .isin() Method

Great for checking against a list.

df[df['Status'].isin(['Active', 'Pending'])]
PYTHON PLAYGROUND
⏳ Loading editor…

Lesson 14: GroupBy Deep Dive

groupby() involves three steps: Split, Apply, Combine.

  1. Split data into groups based on criteria.
  2. Apply a function (sum, mean, count) to each group.
  3. Combine the results.
# Total sales per region
sales_by_region = df.groupby('Region')['Sales'].sum()

Multiple Aggregations: .agg()

You can calculate multiple stats at once!

df.groupby('Region')['Sales'].agg(['sum', 'mean', 'max'])
PYTHON PLAYGROUND
⏳ Loading editor…

Mini Project: Sales Analyst

Challenge:

  1. Create a DataFrame having Category ('Electronics', 'Clothing', 'Electronics'), and Revenue (1000, 500, 1200).
  2. Filter to show only 'Electronics'.
  3. Use groupby to calculate the total Revenue for each Category.

Quiz

Question 1 of 5

Which syntax is correct for filtering with multiple conditions?

df[df['A'] > 5 and df['B'] < 10]
df[(df['A'] > 5) & (df['B'] < 10)]
df[df['A'] > 5 & df['B'] < 10]
df.filter(A > 5, B < 10)

Key Takeaways

Boolean masking (&, |) is the standard way to filter.
groupby() is powerful: think "Split-Apply-Combine".
✅ Use .agg() to get multiple summary statistics at once.