且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas groupby(),agg()-如何在没有多重索引的情况下返回结果?

更新时间:2022-11-04 11:15:48

以下通话:

>>> gr = df.groupby(['EVENT_ID', 'SELECTION_ID'], as_index=False)
>>> res = gr.agg({'ODDS':[np.min, np.max]})
>>> res
    EVENT_ID SELECTION_ID ODDS     
                          amin amax
0  100429300      5297529   18   25
1  100429300      5297559   30   38

返回索引为的框架.如果您不希望列成为多索引,则可以执行以下操作:

returns a frame with mulit-index columns. If you do not want columns to be multi-index either you may do:

>>> res.columns = list(map(''.join, res.columns.values))
>>> res
    EVENT_ID  SELECTION_ID  ODDSamin  ODDSamax
0  100429300       5297529        18        25
1  100429300       5297559        30        38