简体   繁体   English

使用python创建多列的虚拟变量

[英]Create dummy variable of multiple columns with python

I am working with a dataframe containing two columns with ID numbers.我正在处理一个包含两列 ID 号的数据框。 For further research I want to make a sort of dummy variables of these ID numbers (with the two ID numbers).为了进一步研究,我想对这些 ID 号(带有两个 ID 号)制作一种虚拟变量。 My code, however, does not merge the columns from the two dataframes.但是,我的代码不会合并来自两个数据帧的列。 How can I merge the columns from the two dataframes and create the dummy variables?如何合并两个数据框中的列并创建虚拟变量?

Dataframe数据框

import pandas as pd
import numpy as np
d = {'ID1': [1,2,3], 'ID2': [2,3,4]}
df = pd.DataFrame(data=d)

Current code当前代码

pd.get_dummies(df, prefix = ['ID1', 'ID2'], columns=['ID1', 'ID2'])

Desired output期望输出

p = {'1': [1,0,0], '2': [1,1,0], '3': [0,1,1], '4': [0,0,1]}
df2 = pd.DataFrame(data=p)
df2

Different ways of skinning a cat;给猫剥皮的不同方法; here's how I'd do it—use an additional groupby :这是我的做法 - 使用额外的groupby

# pd.get_dummies(df.astype(str)).groupby(lambda x: x.split('_')[1], axis=1).sum()
pd.get_dummies(df.astype(str)).groupby(lambda x: x.split('_')[1], axis=1).max()

   1  2  3  4
0  1  1  0  0
1  0  1  1  0
2  0  0  1  1

Another option is stack ing, if you like conciseness:另一种选择是stack ing,如果你喜欢简洁:

# pd.get_dummies(df.stack()).sum(level=0)
pd.get_dummies(df.stack()).max(level=0)

   1  2  3  4
0  1  1  0  0
1  0  1  1  0
2  0  0  1  1

If need indicators in output use max , if need count values use sum after get_dummies with another parameters and casting values to strings:如果需要输出中的指标使用max ,如果需要计数值在get_dummies之后使用sum和另一个参数并将值转换为字符串:

df = pd.get_dummies(df.astype(str), prefix='', prefix_sep='').max(level=0, axis=1)
#count alternative 
#df = pd.get_dummies(df.astype(str), prefix='', prefix_sep='').sum(level=0, axis=1)
print (df)
   1  2  3  4
0  1  1  0  0
1  0  1  1  0
2  0  0  1  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM