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

前端 未结 5 2075
清歌不尽
清歌不尽 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条回答
  •  渐次进展
    2020-12-14 00:59

    See this enhancement issue

    The short answer is yes, the groupby will preserve the orderings as passed in. You can prove this by using your example like this:

    In [20]: df.sort_index(ascending=False).groupby('A').agg([np.mean, lambda x: x.iloc[1] ])
    Out[20]: 
               B             C         
            mean  mean 
    A                                  
    group1  11.0       10  101      100
    group2  17.5       10  175      100
    group3  11.0       10  101      100
    

    This is NOT true for resample however as it requires a monotonic index (it WILL work with a non-monotonic index, but will sort it first).

    Their is a sort= flag to groupby, but this relates to the sorting of the groups themselves and not the observations within a group.

    FYI: df.groupby('A').nth(1) is a safe way to get the 2nd value of a group (as your method above will fail if a group has < 2 elements)

提交回复
热议问题