我有一个包含2列的数据框,我要添加第3列。

我希望第3列依赖于第2列的值,返回该列的设置答案或相应的索引。

数据库的示例如下:

print (df)
            Amount      Percentage
Country
Belgium      20           .0952
France       50           .2380
Germany      60           .2857
UK           80           .3809


现在,我希望新的第三列中的百分比小于25%时说“其他”,而百分比大于25%时说国家的名字。这就是我写的:

df.['Country']='Other')
df.loc[df['percentage']>0.25, 'Country']=df.index


不幸的是,我的输出没有给出等效的索引。它只是按顺序给出索引:

 print (df)
            Amount      Percentage      Country
Country
Belgium      20           .0952         Other
France       50           .2380         Other
Germany      60           .2857         Belgium
UK           80           .3809         France


显然,我希望看到德国对面的德国和英国对面的英国。
如何获得索引,使其与代码中超出阈值的数字位于同一行?

最佳答案

您可以尝试numpy.where

df['Country'] = np.where(df['Percentage']>0.25, df.index, 'Other')
print df
         Amount  Percentage  Country
Country
Belgium      20      0.0952    Other
France       50      0.2380    Other
Germany      60      0.2857  Germany
UK           80      0.3809       UK


或通过Seriesindex创建to_series

df['Country']='Other'
df.loc[df['Percentage']>0.25, 'Country']=df.index.to_series()
print df
         Amount  Percentage  Country
Country
Belgium      20      0.0952    Other
France       50      0.2380    Other
Germany      60      0.2857  Germany
UK           80      0.3809       UK

10-04 14:42