Pandas DataFrame Group by one Column and Aggregate using MAX, MIN, MEAN and MEDIAN

Kontext Kontext event 2022-09-04 visibility 1,854
more_vert

Code description

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

Code snippet

import pandas as pd

categories = []
values = []
for i in range(0,100):
	categories.append(chr(i%10+65))
	values.append(i)
	
df = pd.DataFrame({'category': categories, 'value':values})
print(df)

# Aggregate
df_agg = df.groupby(by=['category']) \
			.aggregate({"value":
			['max', 'min', 'mean', 'median']})

print(df_agg)
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts