The dataset for this lesson
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:
- Split the rows into groups by some column.
- Apply a function to each group.
- Combine the answers into one result.
print(df.groupby("branch")["marks"].mean())
branch
CSE 79.000000
ECE 71.500000
MECH 41.000000
More than one statistic at a time: