Flatten Pandas DataFrame after Aggregate

event 2022-09-04 visibility 1,517 comment 0 insights
more_vert
insights Stats
Kontext Kontext Code Snippets & Tips

Code snippets and tips for various programming languages/frameworks. All code examples are under MIT or Apache 2.0 license unless specified otherwise. 

Code description

In code snippet Pandas DataFrame Group by one Column and Aggregate using MAX, MIN, MEAN and MEDIAN, it shows how to do aggregations in a pandas DataFrame. This code snippet shows you how to flatten the DataFrame (multiindex) after aggregations. 

Sample output:

  category  value_max  value_min  value_mean  value_median
0        A         90          0          45            45
1        B         91          1          46            46
2        C         92          2          47            47
3        D         93          3          48            48
4        E         94          4          49            49
5        F         95          5          50            50
6        G         96          6          51            51
7        H         97          7          52            52
8        I         98          8          53            53
9        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']})
# Flatten DataFrame
df_agg.columns = ['_'.join(col) for col in df_agg.columns]
df_agg = df_agg.reset_index()
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