gpt4 book ai didi

python - 对多个列进行分组并在组内排名

转载 作者:行者123 更新时间:2023-12-01 06:23:48 25 4
gpt4 key购买 nike

我正在寻找组内的设定排名,但不知道该怎么做。

Below is the test data, 
df = pd.DataFrame()

names = ['California','New York','California','New York', 'California','New York', 'California', 'California', 'California', 'California']
types = ['Student','Student','Student','Student','Student', 'Pleasure', 'Pleasure', 'Pleasure','Business', 'Business']

df['names'] = names
df['type'] = types

df.groupby(['names', 'type']).size().reset_index(name='counts')

Below is the output
names type counts
0 California Business 2
1 California Pleasure 2
2 California Student 3
3 New York Pleasure 1
4 New York Student 2

我想得到以下输出,排名基于列名称和计数(desc)。在加利福尼亚州,“商务与休闲”具有相同的计数,对我来说,结果排名是 2,3 还是 2,2 并不重要。

        names   type      counts Rank
0 California Business 2 2
1 California Pleasure 2 2
2 California Student 3 1
3 New York Pleasure 1 2
4 New York Student 2 1

任何想法/解决方案

谢谢

最佳答案

使用GroupBy.rank通过 Series.astype 将 float 转换为整数:

df['Rank'] = df.groupby('names')['counts'].rank(ascending=False, method='dense').astype(int)
print(df)
names type counts Rank
0 California Business 2 2
1 California Pleasure 2 2
2 California Student 3 1
3 New York Pleasure 1 2
4 New York Student 2 1

关于python - 对多个列进行分组并在组内排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60262687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com