How to properly use the `pivot_table` function with multiple aggregation functions on a DataFrame in Pandas?
I'm following best practices but I'm attempting to create a pivot table in Pandas using the `pivot_table` function, but I'm running into issues when trying to apply multiple aggregation functions to the same column. My DataFrame contains sales data with columns for `Date`, `Category`, `Revenue`, and `Units Sold`. Specifically, I want to calculate both the total revenue and the average units sold for each category on a monthly basis. Hereβs a snippet of my DataFrame: ```python import pandas as pd data = { 'Date': ['2023-01-15', '2023-01-20', '2023-02-10', '2023-02-15'], 'Category': ['A', 'A', 'B', 'B'], 'Revenue': [200, 300, 150, 400], 'Units Sold': [2, 3, 1, 5] } df = pd.DataFrame(data) df['Date'] = pd.to_datetime(df['Date']) ``` Now, I'm trying to create the pivot table like this: ```python pivot_df = df.pivot_table(index=df['Date'].dt.to_period('M'), columns='Category', values=['Revenue', 'Units Sold'], aggfunc={'Revenue': 'sum', 'Units Sold': 'mean'}) ``` However, I receive the following behavior: ``` ValueError: "If using multiple values, you must specify a nested dictionary for `aggfunc`." ``` I've tried wrapping the `aggfunc` argument in a list and other variations, but nothing seems to work. Whatβs the correct way to do this? I want the resulting pivot table to show the total revenue and the average units sold for each category on a monthly basis, but I'm not sure how to structure the `aggfunc` parameter correctly for multiple columns. Any insights would be greatly appreciated!