Python Pandas - Only showing rows in DF for the MAX values of a column

六眼飞鱼酱① 提交于 2021-02-05 08:54:48

问题


searched for this, but cannot find an answer.

Say I have a dataframe (apologies for formatting):

a Dave $400
a Dave $400
a Dave $400
b Fred $220
c James $150
c James $150
d Harry $50

And I want to filter the dataframe so it only shows the rows where the third column is the MAXIMUM value, could someone point me in the right direction?

i.e. it would only show Dave's rows

All I can find is ways of showing the rows where its the maximum value for each separate index (the indexes being A, B, C etc)

Thank you in advance


回答1:


This should work:

>>> df
       0  money
a   Dave    400
a   Dave    400
a   Dave    400
b   Fred    220
c  James    150
c  James    150
d  Harry     50

>>> df[df['money'] == df['money'].max()]
      0  money
a  Dave    400
a  Dave    400
a  Dave    400



回答2:


If your index is unique and you are OK with returning one row (in the case of multiple rows having the same max value) then you can use the idxmax method.

df.loc[df['money'].idxmax()]

And if you want to add some flare you can highlight the max value in each column with:

df.loc[df['money'].idxmax()].style.highlight_max()



来源:https://stackoverflow.com/questions/40959626/python-pandas-only-showing-rows-in-df-for-the-max-values-of-a-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!