我有一个DataFrame(df),其值如下:
Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Germany 13
Singapore 11
Canada 10
我想添加值为<的所有国家/地区25,并用它们的总和(34)将它们显示为“其他”。
我已通过以下代码为国家/地区创建了列名:
df1 = df.rename_axis('fintech_countries').rename_axis("countries", axis="columns" , inplace=True)
countries Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Germany 13
Singapore 11
Canada 10
现在,我已根据StackOverflow上的另一个查询尝试了以下代码:
df1.loc[df1['Title'] < 25, "countries"].sum()
但是我收到以下错误:
KeyError: 'the label [countries] is not in the [columns]'
有人可以帮忙吗?我需要最终输出:
countries Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Others 34
TIA
答案 0 :(得分:3)
针对loc
的setting with enlargement和boolean indexing
过滤的解决方案:
mask = df['Title'] < 25
print (mask)
fintech_countries
US False
UK False
India False
Australia False
Germany True
Singapore True
Canada True
Name: Title, dtype: bool
df1 = df[~mask].copy()
df1.loc['Others', 'Title'] = df.loc[mask, 'Title'].sum()
df1.Title = df1.Title.astype(int)
print (df1)
countries Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Others 34