011. The .agg() Method
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
The .agg() (or .aggregate()) method is chained onto a GroupBy object. Instead of applying a hardcoded function, it takes instructions on *what* functions to apply. Passing a list of strings like ['min', 'max', 'sum'] will instantly calculate all three.
022. Dictionary Mapping
The true power of .agg() comes from passing Python dictionaries. You can specify different logic for different columns. For example: df.groupby('Country').agg({'Population': 'sum', 'GDP': 'mean'}). This prevents Pandas from doing useless math (like summing a latitude column).
033. MultiIndex Columns
When you apply multiple functions to the same column, Pandas cannot use a flat table structure anymore. It creates a 'MultiIndex' for the columns (a hierarchy where 'Revenue' is the top level, and 'mean'/'sum' are sub-columns). This makes the table look like an Excel Pivot Table.
?Frequently Asked Questions
Can I use custom functions in .agg()?
Yes! Instead of passing the string `'mean'`, you can pass a reference to a custom Python function you wrote, or even a lambda function, like `.agg(lambda x: x.max() - x.min())`.
How do I flatten MultiIndex columns?
MultiIndex columns can be annoying to work with programmatically. You can flatten them by joining the levels: `df.columns = ['_'.join(col) for col in df.columns]`.
