GroupBy and Aggregation
df.groupby(), agg(), transform(), apply(), named aggregations with Named Aggregation.
The Split-Apply-Combine Idea
GroupBy follows the split-apply-combine pattern: split rows into groups by a key, apply a function to each group, then combine the results into one frame. It powers most analytical summaries.
import pandas as pd
df = pd.DataFrame({
"dept": ["A", "A", "B", "B", "B"],
"salary": [50, 70, 40, 60, 80],
})Basic groupby
df.groupby("col") creates a grouped object. Chaining an aggregation like .mean() computes one value per group.
print(df.groupby("dept")["salary"].mean())
# dept
# A 60.0
# B 60.0All lessons in this course
- GroupBy and Aggregation
- Pivot Tables and Cross-Tabulation
- Advanced Merging and Joining
- Time Series in Pandas