FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses Python for Data Analysis

AI & DS

Progress0 / 6 lessons
  1. 1. NumPy arrays and why they beat lists
  2. 2. pandas Series and DataFrame
  3. 3. Loading, cleaning and handling missing data honestly
  4. 4. Filtering, groupby and merge
  5. 5. Simple statistics and what they hide
  6. 6. Plotting basics, and how not to mislead with a chart

Courses › Python for Data Analysis

Filtering, groupby and merge

Split, summarise, and join two tables without silently losing rows.

13 min read · Lesson 4 of 6 · Pro

The dataset for this lesson

Python 3
import pandas as pd

df = pd.DataFrame({
    "roll":   [101, 102, 103, 104, 105, 106],
    "name":   ["Ravi", "Sneha", "Arjun", "Meera", "Imran", "Divya"],
    "branch": ["CSE", "CSE", "ECE", "ECE", "MECH", "CSE"],
    "sem":    [3, 3, 5, 5, 3, 5],
    "marks":  [78, 92, 55, 88, 41, 67],
})

groupby is split, apply, combine

Three steps every time:

  1. Split the rows into groups by some column.
  2. Apply a function to each group.
  3. Combine the answers into one result.
Python 3
print(df.groupby("branch")["marks"].mean())
Code
branch
CSE     79.000000
ECE     71.500000
MECH    41.000000

More than one statistic at a time:

The rest of this lesson is Pro

The free lessons of Python for Data Analysis finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.