python - Find maximum value of a column and return the corresponding row values using Pandas -


structure of data;

using python pandas trying find 'country' & 'place' maximum value.

this returns maximum value:

data.groupby(['country','place'])['value'].max() 

but how corresponding 'country' , 'place' name?

assuming df has unique index, gives row maximum value:

in [34]: df.loc[df['value'].idxmax()] out[34]:  country        place      kansas value         894 name: 7 

note idxmax returns index labels. if dataframe duplicates in index, label may not uniquely identify row, df.loc may return more 1 row.

therefore, if df not have unique index, must make index unique before proceeding above. depending on dataframe, can use stack or set_index make index unique. or, can reset index (so rows become renumbered, starting @ 0):

df = df.reset_index() 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -