This code snippet provides one example of grouping a pandas DataFrame by one column and then aggregating on multiple columns using different functions including max
, min
, mean
and median
.
We pass in a dictionary for each column that needs to be aggregated: the key is the column name, and the value is a list of aggregation functions supported by pandas DataFrame. The result DataFrame will have multiple levels as following output shows:
Sample output:
value
max min mean median
category
A 90 0 45 45
B 91 1 46 46
C 92 2 47 47
D 93 3 48 48
E 94 4 49 49
F 95 5 50 50
G 96 6 51 51
H 97 7 52 52
I 98 8 53 53
J 99 9 54 54