Formation 90 min read Data Manipulation with Pandas

πŸ“¦ Groupby and Aggregation with Pandas

Python & Data Science Chapter : Data Manipulation with Pandas Sub-chapter : Groupby and aggregation

Learning objectives

🎯 Objectives:\n
1Use groupby\n2. Apply aggregations\n3. Use agg()\n4. Understand transform()

Introduction

πŸ“– Groupby allows analyzing data by category.

Theoretical content

Groupby:\n
PYTHON
\ndf.groupby("category").mean()\n

Practical examples

πŸ’» Example: Sales analysis by region\n
PYTHON
\nsales.groupby("region")["sales"].sum()\n

Best practices

1Use agg() for multiple aggregations\nβœ… 2. Use transform() to add columns\nβœ… 3. Use reset_index() to get normal DataFrame\nβœ… 4. Use as_index=False in groupby

Common pitfalls

Forgetting that groupby returns a GroupBy object\n
Apply an aggregation after groupby

Summary

groupby(): grouping\nβœ… agg(): multiple aggregations\nβœ… transform(): add columns\nβœ… reset_index(): normal DataFrame

Additional resources

πŸ“š pandas.pydata.org/docs/user_guide/groupby.html