Python (Pandas) 错误 'the label [Algeria] is not in the [index]'

Python (Pandas) error 'the label [Algeria] is not in the [index]'

我不明白为什么会这样

df[(df['Gold']>0) & (df['Gold.1']>0)].loc[((df['Gold'] - df['Gold.1'])/(df['Gold'])).abs().idxmax()]

但是当我除以 (df['Gold'] + df['Gold.1'] + df['Gold.2']) 它停止工作给我错误,您可以在下面找到。

有趣的是,以下行有效

df.loc[((df['Gold'] - df['Gold.1'])/(df['Gold'] + df['Gold.1'] + df['Gold.2'])).abs().idxmax()]

刚开始学习Python和Pandas,不明白是怎么回事。我需要了解发生这种情况的原因以及如何解决它。

错误

KeyError: 'the label [Algeria] is not in the [index]'

DataFrame 快照

你的问题是boolean indexing:

df[(df['Gold']>0) & (df['Gold.1']>0)]

returns 一个过滤后的 DataFrame,它不包含 maxindexSeries 你用这个计算的:

((df['Gold'] - df['Gold.1'])/(df['Gold'] + df['Gold.1'] + df['Gold.2'])).abs().idxmax()

在您的数据中是 Algeria

所以 loc 逻辑上抛出 KeyError.

一种可能的解决方案是将新过滤的DataFrame分配给df1,然后使用idxmax得到Series的最大值对应的索引:

df1 = df[(df['Gold']>0) & (df['Gold.1']>0)]
df2 = df1.loc[((df1['Gold']-df1['Gold.1'])/(df1['Gold']+df1['Gold.1']+df1['Gold.2'])).abs().idxmax()]