Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫-从分类列创建布尔列_Python_Pandas_Dataframe - Fatal编程技术网

Python 熊猫-从分类列创建布尔列

Python 熊猫-从分类列创建布尔列,python,pandas,dataframe,Python,Pandas,Dataframe,我在Pandas dataframe中有一列Place,如下所示: **Place** Berlin Prague Mexico Prague Mexico ... 我想做以下几点: is_Berlin is_Prague is_Mexico 1 0 0 0 1 0 0 0 1 0 1 0 0 0

我在Pandas dataframe中有一列Place,如下所示:

**Place**
Berlin
Prague
Mexico
Prague
Mexico
...
我想做以下几点:

is_Berlin   is_Prague   is_Mexico
1           0           0
0           1           0
0           0           1
0           1           0
0           0           1   
我知道我可以单独创建列:

df['is_Berlin'] = df['Place']
df['is_Prague'] = df['Place']
df['is_Mexico'] = df['Place']
然后为每列创建一个字典并应用映射函数

#Example just for is_Berlin column
d = {'Berlin': 1,'Prague': 0,'Mexico': 0} 
df['is_Berlin'] = df['is_Berlin'].map(d)
但我觉得这有点乏味,我相信有一种很好的python方法可以做到这一点。

您可以使用,如果需要,可以将这些新列添加到原始的
数据框架中,使用:


为什么要在有副本时回答?副本?你能给我一个链接吗?但有可能,我首先回答,永远不会找到重复的:(.太好了!有没有办法将它们添加到现有的数据框架中?列位置实际上是我的数据框架的一部分,我想用这3个新列替换它。
df1 = df.Place.str.get_dummies()
print df1
   Berlin  Mexico  Prague
0       1       0       0
1       0       0       1
2       0       1       0
3       0       0       1
4       0       1       0

df1.columns = ['is_' + col for col in df1.columns]
print df1
   is_Berlin  is_Mexico  is_Prague
0          1          0          0
1          0          0          1
2          0          1          0
3          0          0          1
4          0          1          0
df = pd.concat([df, df1], axis=1)
print df
    Place  is_Berlin  is_Mexico  is_Prague
0  Berlin          1          0          0
1  Prague          0          0          1
2  Mexico          0          1          0
3  Prague          0          0          1
4  Mexico          0          1          0

#if there is more columns, you can drop Place column
df = df.drop('Place', axis=1)
print df
   is_Berlin  is_Mexico  is_Prague
0          1          0          0
1          0          0          1
2          0          1          0
3          0          0          1
4          0          1          0