数据框分组依据-每列分别汇总

时间:2019-08-13 21:07:37

标签: python dataframe pandas-groupby

我想对dataframegroupby对象的每一列使用不同的聚合函数

按某些字段将数据框分组并汇总一些列

def aggregation(df):
  quorum = df.groupby("itemId")[['Xmin','Ymin', 'Xmax', 'Ymax']].median().reset_index()
return quorum


I want to make aggregation min() for 'Xmin','Ymin' fields and max() for 'Xmax', 'Ymax' ones. But don't know how to write desirable code...

1 个答案:

答案 0 :(得分:2)

def aggregation(df):
    return df[['itemId','Xmin','Ymin', 'Xmax', 'Ymax']].groupby("itemId").agg({'Xmin':'min','Ymin':'min','Xmax':'max','Ymax':'max'}

其中Xmin,Ymin Xmax,Ymax是各自的列名