Reputation: 10026
I need to supply a tuple with the highest value from a column (yield) and the associated index value in the form (index, value)
I came up with:
t= (df['yield'].idxmax(),df['yield'].max())
But I'm wondering is there a nicer way to get these values (in a tuple)?
Upvotes: 1
Views: 2107
Reputation: 164843
Here is an alternative way. It is not a one-liner, but it avoids the need to effectively calculate the maximum twice.
import pandas as pd
df = pd.DataFrame({'yield': [0, 1, 3, 5, 1, 9, 5]})
s = df.loc[df['yield'] == df['yield'].max(), 'yield']
res = next(zip(s.index, s))
print(res)
# (5, 9)
Alternatively:
s = df.loc[df['yield'].argmax()]
res = (s.name, s.iloc[0])
Upvotes: 1