pandas -从分类列创建布尔列 [英] Pandas - create boolean columns from categorical column
本文介绍了 pandas -从分类列创建布尔列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Pandas数据框中有Place一栏,如下所示:
I have column Place in Pandas dataframe which looks like this:
**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
我知道我可以单独创建列:
I know I can create the columns separately:
df['is_Berlin'] = df['Place']
df['is_Prague'] = df['Place']
df['is_Mexico'] = df['Place']
然后为每列创建一个字典并应用一个映射函数。
And then create a dictionary for each column and apply a map function.
#Example just for is_Berlin column
d = {'Berlin': 1,'Prague': 0,'Mexico': 0}
df['is_Berlin'] = df['is_Berlin'].map(d)
但是我觉得这有点乏味,我相信
But I find this somehow tedious and I believe there is nice pythonic way how do to it.
推荐答案
您可以使用 str.get_dummies
,如果需要将此新列添加到原始 DataFrame
中,使用 concat
:
You can use str.get_dummies
and if need add this new columns to original DataFrame
, use concat
:
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
这篇关于 pandas -从分类列创建布尔列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文