简体   繁体   English

如何对 Pandas GroupBy 对象中的组进行排序?

[英]How can I order the groups in a pandas GroupBy object?

I have a pandas DataFrame with 3 columns: Date, Concept and Value.我有一个包含 3 列的 Pandas DataFrame:日期、概念和值。 I want to add a fourth column "Rank" that contains the order statistic of Value when grouped by Date.我想添加第四列“排名”,其中包含按日期分组时价值的订单统计信息。 So for example, consider the following table:例如,请考虑下表:

Date日期 Concept概念 Value价值
2021-01-01 2021-01-01 A一种 20 20
2021-01-01 2021-01-01 B 30 30
2021-01-01 2021-01-01 C C 25 25
2021-02-01 2021-02-01 A一种 17 17
2021-02-01 2021-02-01 B 31 31
2021-02-01 2021-02-01 C C 42 42

In this case, my final table should be this:在这种情况下,我的决赛桌应该是这样的:

Date日期 Concept概念 Value价值 Rank
2021-01-01 2021-01-01 A一种 20 20 1 1
2021-01-01 2021-01-01 B 30 30 3 3
2021-01-01 2021-01-01 C C 25 25 2 2
2021-02-01 2021-02-01 A一种 17 17 1 1
2021-02-01 2021-02-01 B 31 31 2 2
2021-02-01 2021-02-01 C C 42 42 3 3

Is there any elegant way to do it with pandas?有没有什么优雅的方法可以用熊猫来做到这一点?

Use transform with pd.Series.rank :transformpd.Series.rank一起pd.Series.rank

df = pd.DataFrame({'Date': {0: '2021-01-01 ', 1: '2021-01-01 ', 2: '2021-01-01 ', 3: '2021-02-01 ', 4: '2021-02-01 ', 5: '2021-02-01 '}, 
                   'Concept ': {0: 'A ', 1: 'B ', 2: 'C ', 3: 'A ', 4: 'B ', 5: 'C '}, 
                   'Value': {0: 20, 1: 30, 2: 25, 3: 17, 4: 31, 5: 42}})

df['Rank'] = df.groupby(['Date'])['Value'].transform(pd.Series.rank)
          Date Concept   Value  Rank
0  2021-01-01        A      20     1
1  2021-01-01        B      30     3
2  2021-01-01        C      25     2
3  2021-02-01        A      17     1
4  2021-02-01        B      31     2
5  2021-02-01        C      42     3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM