groupby()常用于对一类特征值相同的对象进行总体操作的方法
1.导入pandas,自己定义一个dataframe
import pandas as pd
df = pd.DataFrame({'key':['A','B','C','A','B','C','A','B','C'],
'data':[0,5,10,5,10,15,10,15,20]})
df
2.求A、B、C各自的总合
for key in ['A','B','C']:
print (key,df[df['key'] == key].sum())
df.groupby('key').sum()
3.读取csv
df = pd.read_csv('./data/titanic.csv')
df.groupby('Sex')['Age'].mean()
df.groupby('Sex')['Survived'].mean()