我有一个熊猫数据框,如下所示。
df1 = {'City' :['London', 'Basel', 'Kairo'], 'Country': ['UK','Switzerland','Egypt']}
City Country
0 London UK
1 Basel Switzerland
2 Kairo Egypt
现在,我要分配一个新列,称为“ Continent”,应基于这些值进行填充。
所以df2应该看起来像:
City Country Continent
0 London UK Europe
1 Basel Switzerland Europe
2 Kairo Egypt Africa
因此df1为df2中的新列设置了值。如何以最简单的方式做到这一点?
答案 0 :(得分:0)
使用country-> continent字典,您可以将其传递给pd.DataFrame.replace()方法,如下所示:
df['Continent'] = df['Country'].replace(country_dict)