问题
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