我在Pandas dataframe中有一个列Place,如下所示:
**Place**
Berlin
Prague
Mexico
Prague
Mexico
...
我想做以下事情:
^{pr2}$我知道我可以单独创建列:
df['is_Berlin'] = df['Place']
df['is_Prague'] = df['Place']
df['is_Mexico'] = df['Place']
然后为每个列创建一个字典并应用map函数。在
#Example just for is_Berlin column
d = {'Berlin': 1,'Prague': 0,'Mexico': 0}
df['is_Berlin'] = df['is_Berlin'].map(d)
但我觉得这有点乏味,我相信有一种很好的Python式的方法。在
您可以使用^{} ,如果需要将此新列添加到原始的} :
^{pr2}$DataFrame
,请使用^{相关问题 更多 >
编程相关推荐