Pandas - 根据其他列中的最小值获取值

use*_*628 2 python pandas

我有以下数据框:

      Q        GDP  
248 2008q3  14891.6 
249 2008q4  14577.0 
250 2009q1  14375.0 
251 2009q2  14355.6 
Run Code Online (Sandbox Code Playgroud)

我想要 GDP 最低的 Q 值。

基于这篇文章,根据另一列熊猫数据框提取列值,我尝试了以下操作:

    df = df.loc[df['GDP'].min(),'Quarters'].iloc[0]
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误消息:

    TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [14355.6] of <class 'numpy.float64'>
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激!

May*_*wal 6

这个:

df.loc[df['GDP'].idxmin()]['Q']
Run Code Online (Sandbox Code Playgroud)

输出:

'2009q2'
Run Code Online (Sandbox Code Playgroud)