-1.类似字典的调用方式
groupby = df.groupby('id')
# 遍历
for id,group in groupby:
print(id)
print(group)
# 取其中一组
data = groupby.get_group('aaa')
-2.单列函数
df_groupby = df.groupby('id')['salary'].sum()
df_groupby = df.groupby(['id','gender'])['salary'].sum()
mean, min, max, count, std,
https://www.cnblogs.com/Yanjy-OnlyOne/p/11217802.html
-3.agg
df_groupby = df.groupby('id')['salary'].agg(['max','min','std'])
-4.自定义函数
效果:各类别高于3倍标准差为1,否则为0
def handle_abnormal(df, shred=3):
df_temp = df[df>(df.mean()+ shred *df.std())]
if not df_temp.empty:
return 1
else:
return 0
df_beh_abnormal = df_beh_date.groupby('id')['count'].apply(handle_abnormal)