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
- Create
dfas above. - Run
groupby("city")["amount"].sum(). - Build the named aggregation
summary. - 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.
Ravindra Bagale's Tip – मराठी
groupby(...)["amount"].sum() नंतर Series मिळते, आणि group ची नावं normal columns ऐवजी index मध्ये जातात. Report table साठी as_index=False सोबत gmv=("amount", "sum") सारखं named aggregation वापरा. मग प्रत्येक result column ला स्पष्ट नाव मिळतं, आणि ते sort, filter किंवा export करणं सोपं जातं.
Ravindra Bagale's Tip – हिंदी
groupby(...)["amount"].sum() के बाद Series मिलती है, और group के नाम normal columns की जगह index में चले जाते हैं। Report table के लिए as_index=False के साथ gmv=("amount", "sum") जैसा named aggregation लगाओ। फिर हर result column को साफ़ नाम मिलता है, और उसे sort, filter या export करना आसान हो जाता है।