简体   繁体   English

python-打印每个组的前5个

[英]python- print top 5 from each group

Need to find the popular names, which are grouped by sex 需要找到按性别分组的流行名称

bnames_decade = bnames_decade.groupby(['sex','name'])['births'].sum().sort_values(ascending=False)

this shows 由此可见

F    Emma        121375
     Sophia      117352
     Olivia      111691
M    Noah        110280
     Mason       105104
     Jacob       104722
F    Isabella    103947

... ...

I want to print top5 names of each group. 我想打印每个组的top5名称。 Can anyone suggest a Python coding for this? 有人可以为此建议Python编码吗?

The way I tried is not working. 我尝试的方式不起作用。

bnames_top5 =bnames_decade.groupby('sex').head(5)
import pandas as pd
bnames_decade = pd.DataFrame([['F','Emma',121375],['F','Sophia',117352],['F','Olivia',111691],['M','Noah',110280],['M','Mason',105104],['F','Isabella',103947], ['F','Isabella2',103946],['F','Isabella3',103945],['F','Isabella4',103944],['M','Isabella5',103943],['M','Isabella6',103942],['M','Isabella7',103941],['M','Isabella8',103940]], columns=['sex','name','births'])
print(bnames_decade)

在此处输入图片说明

for key, group in bnames_decade.groupby(['sex']):
    print(group['name'].iloc[0:5])

在此处输入图片说明

One idea is to use group by sex and name and sort descending. 一种想法是使用按sexname分组并按降序排序。 Then perform another GroupBy with head . 然后用head执行另一个GroupBy Here's an example: 这是一个例子:

df = pd.DataFrame({'sex': ['F', 'F', 'F', 'M', 'M', 'M', 'F', 'F', 'F', 'M', 'M', 'M'],
                   'name': ['Ursula', 'Jane', 'Edith', 'Leo', 'Brian', 'Philip',
                            'Ursula', 'Edith', 'Daphne', 'Leo', 'Brian', 'George']})

df = df.groupby(['sex', 'name']).size().sort_values(ascending=False).reset_index()

res = df.groupby('sex').head(2)

print(res)

  sex    name  0
0   M     Leo  2
1   M   Brian  2
2   F  Ursula  2
3   F   Edith  2

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

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