在DataFrame.groupby的情况下,如何基于另一列的最大值获取列的值 [英] How to get value of a column based on the maximum of another column in case of DataFrame.groupby

查看:60
本文介绍了在DataFrame.groupby的情况下,如何基于另一列的最大值获取列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框.

I have a dataframe which looks like this.

id年发布的歌手数168 2015年缪斯女神1169 2015蕾哈娜3170 2015泰勒·斯威夫特2171 2016珍妮弗·洛佩兹1172 2016蕾哈娜3173 2016黑夜传说1174 2017年酷玩乐队1175 2017年Ed Sheeran 2

我想获得每年的最高人数,然后获得相应的艺术家姓名.

I want to get the maximum count for each year and then get the corresponding Artist name.

类似这样的东西:

年度发行歌手

2015蕾哈娜
2016蕾哈娜(Rihanna)
2017埃德·希兰(Ed Sheeran)

2015 Rihanna
2016 Rihanna
2017 Ed Sheeran

我尝试使用循环遍历数据框的行,并创建另一个字典,其键为年,值为艺术家.但是,当我尝试将该字典转换为数据框时,键将映射到列而不是行.

I have tried using a loop to iterate over the rows of the dataframe and create another dictionary with key as year and value as artist. But when I try to convert that dictionary to a dataframe, the keys are mapped to columns instead of rows.

有人可以指导我有一个更好的方法,而不必遍历数据框,而是使用一些内置的pandas方法来实现这一目标吗?

Can somebody guide me to have a better approach to this without having to loop over the dataframe and instead use some inbuilt pandas method to achieve this?

推荐答案

查看 idxmax

df.loc[df.groupby('YearReleased')['count'].idxmax()]
Out[445]: 
    id  YearReleased     Artist  count
1  169          2015    Rihanna      3
4  172          2016    Rihanna      3
7  175          2017  EdSheeran      2

这篇关于在DataFrame.groupby的情况下,如何基于另一列的最大值获取列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆