Python Pandas - Only Showing Rows In Df For The Max Values Of A Column
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
Solution 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
Solution 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()
Post a Comment for "Python Pandas - Only Showing Rows In Df For The Max Values Of A Column"