To perform multiple grouped summaries in Python using pandas, you can use the groupby() method in combination with the agg() method.
Assuming you have a pandas DataFrame with columns that you want to group by and summarize, you can use the following syntax:
import pandas as pd# create a pandas DataFramedf = pd.DataFrame({'Group': ['A', 'B', 'C', 'A', 'B', 'C'], 'Value1': [1, 2, 3, 4, 5, 6], 'Value2': [10, 20, 30, 40, 50, 60]})# group by the 'Group' column and calculate the mean of the 'Value1' column and sum of the 'Value2' columngrouped = df.groupby('Group').agg({'Value1': 'mean', 'Value2': 'sum'})print(grouped) |
This will group the DataFrame by the 'Group' column and calculate the mean of the 'Value1' column and sum of the 'Value2' column for each group. The resulting output will be:
Value1 Value2Group A 2.5 50B 3.5 70C 4.5 90 |
You can add as many columns and aggregation functions as you need inside the dictionary passed to the agg() method.