user2629628
user2629628

Reputation: 141

Pandas - Get value based on minimum value in other column

I have the following dataframe:

      Q        GDP  
248 2008q3  14891.6 
249 2008q4  14577.0 
250 2009q1  14375.0 
251 2009q2  14355.6 

I want to the value for Q where GDP is lowest.

Based on this post,extract column value based on another column pandas dataframe, I tried the following:

    df = df.loc[df['GDP'].min(),'Quarters'].iloc[0]

However I got this following error msg:

    TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [14355.6] of <class 'numpy.float64'>

Any help would be much appreciated!

Upvotes: 6

Views: 9435

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34086

This:

df.loc[df['GDP'].idxmin()]['Q']

Output:

'2009q2'

Upvotes: 13

Related Questions