Ravindra BagaleCourses & study guides मराठी Track your progress

7. GroupBy and Aggregations

Data Science

7.2 Multiple Keys and Named Agg

summary = (
    df.groupby(["city", "festival"], as_index=False)
      .agg(
          gmv=("amount", "sum"),
          orders=("order_id", "nunique"),
          avg_ticket=("amount", "mean"),
      )
)
summary.sort_values("gmv", ascending=False)

Steps in Jupyter

  1. Create df as above.
  2. Run groupby("city")["amount"].sum().
  3. Build the named aggregation summary.
  4. Filter summary[summary["festival"] == "Diwali"].

What you should see. Pune Diwali and Nashik Diwali rows with higher GMV than “None” days — fictional spike pattern.

Ravindra Bagale's Tip

After groupby(...)["amount"].sum() you get a Series, and the group names sit in the index instead of in normal columns. For a report table, use as_index=False with named aggregation like gmv=("amount", "sum"). Every result column then gets a clear name, and you can sort, filter or export it easily.