pandas 如何按一列分组并对另一列的值进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40666126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:27:34  来源:igfitidea点击:

How to group by one column and sort the values of another column?

pythonsortingpandasgroup-by

提问by user1700890

Here is my dataframe

这是我的数据框

import pandas as pd
df = pd.DataFrame({'A': ['one', 'one', 'two', 'two', 'one'] ,
                   'B': ['Ar', 'Br', 'Cr', 'Ar','Ar'] ,
                   'C': ['12/15/2011', '11/11/2001', '08/30/2015', '07/3/1999','03/03/2000' ],
                      'D':[1,7,3,4,5]})

My goal is to group by column Aand sort within grouped results by column B.

我的目标是按列A分组并按列对分组结果进行排序B

Here is what I came up with:

这是我想出的:

sort_group = df.sort_values('B').groupby('A')

I was hoping that grouping operation would not distort order, but it does not work and also returns not a dataframe, but groupbyobject

我希望分组操作不会扭曲顺序,但它不起作用并且返回的不是数据帧,而是groupby对象

<pandas.core.groupby.DataFrameGroupBy object at 0x0000000008B190B8>

Any suggestions?

有什么建议?

回答by Cleb

You cannot apply sort_valuesdirectly to a groupbyobject but you need an apply:

您不能sort_values直接应用于groupby对象,但您需要一个apply

df.groupby('A').apply(lambda x: x.sort_values('B'))

gives you the desired output:

为您提供所需的输出:

         A   B           C  D
A                            
one 0  one  Ar  12/15/2011  1
    4  one  Ar  03/03/2000  5
    1  one  Br  11/11/2001  7
two 3  two  Ar   07/3/1999  4
    2  two  Cr  08/30/2015  3