pandas calculate mean by groups

Solutions on MaxInterview for pandas calculate mean by groups by the best coders in the world

showing results for - "pandas calculate mean by groups"
Marie
19 Jan 2020
1# Basic syntax:
2df.groupby('column_name').mean()
3
4# Where this will return the mean of each group with the same values in
5#	the column "column_name"
6
7# Example usage:
8import pandas as pd
9import numpy as np
10
11df = pd.DataFrame({'A': [1, 1, 2, 1, 2],
12                   'B': [np.nan, 2, 3, 4, 5],
13                   'C': [1, 2, 1, 1, 2]}, columns=['A', 'B', 'C'])
14
15print(df)
16	A	B	C
170	1	NaN	1
181	1	2.0	2
192	2	3.0	1
203	1	4.0	1
214	2	5.0	2
22
23# Calculate the mean of columns B and C grouped by the values in column A
24df.groupby('A').mean() # Returns:
25	B	C
26A		
271	3.0	1.333333
282	4.0	1.500000
29
30# Calculate the mean of column C grouped by the values in columns A and B
31df.groupby(['A', 'B']).mean() # Returns:
32		C
33A	B	
341	2.0	2
35	4.0	1
362	3.0	1
37	5.0	2