Python Pandas: Is Order Preserved When Using groupby() and agg()?

前端 未结 5 2056
清歌不尽
清歌不尽 2020-12-14 00:21

I\'ve frequented used pandas\' agg() function to run summary statistics on every column of a data.frame. For example, here\'s how you would produce the mean an

5条回答
  •  -上瘾入骨i
    2020-12-14 00:37

    In order to preserve order, you'll need to pass .groupby(..., sort=False). In your case the grouping column is already sorted, so it does not make difference, but generally one must use the sort=False flag:

     df.groupby('A', sort=False).agg([np.mean, lambda x: x.iloc[1] ])
    

提交回复
热议问题