import pandas as pd
import numpy as np
df = pd.DataFrame({'a':['one','two','one','two'],
'b':['key1', 'key1','key2','key2'],
'c':[2,3,4,5],
'd':[2,1,2,3],
'e':[3,3,4,4]})
df
1
2
3
4
5
6
7
8
9
# 对a进行分组,使用mean聚合函数,方法1
df.groupby('a').agg(['mean'])
1
2
# 对a进行分组,使用mean聚合函数,方法2
df[['c','d','e']].groupby(df['a']).agg(['mean'])
1
2
# 对a,b进行分组,使用mean,sum聚合函数(http://www.my516.com)
df.groupby(['a','b']).agg(['mean','sum'])
1
2
---------------------