数据集包含 4 列,其中 name 是 child 的名字,yearofbirth 表示 child 出生的年份,number 表示以该特定名字命名的婴儿的数量。
For example, entry 1 reads, in the year 1880, 7065 girl children were named Mary.
通过 pandas,我试图找出每年哪个名字是最常用的。我的代码
df.groupby(['yearofbirth']).agg({'number':'max'}).reset_index()
以上代码部分回答了手头的问题。
我想要名称和最大数量。
最佳答案
基于 this question 的回答我想出了这个解决方案:
idx = df.groupby(['yearofbirth'])['number'].transform(max) == df['number']
df = df[idx]
print(df)
name number sex yearofbirth
0 Mary 7065 F 1880
关于python - Pandas +群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52422195/