Pandas max value index

mGarsteck picture mGarsteck · Oct 10, 2016 · Viewed 54.5k times · Source

I have a Pandas DataFrame with a mix of screen names, tweets, fav's etc. I want find the max value of 'favcount' (which i have already done) and also return the screen name of that 'tweet'

df = pd.DataFrame()
df['timestamp'] = timestamp
df['sn'] = sn
df['text'] = text
df['favcount'] = fav_count


print df
print '------'
print df['favcount'].max()

I cant seem to find anything on this, can anyone help guide me in the right direction?

Answer

Steven G picture Steven G · Oct 10, 2016

Use argmax() idxmax() to get the index of the max value. Then you can use loc

df.loc[df['favcount'].idxmax(), 'sn']

Edit: argmax() is now deprecated, switching for idxmax()