Merging two data frames into a new one with unique items marked with 1 or 0
Using concat
+ get_dummies
u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])
pd.get_dummies(u.Feature).sum(level=0).T
DF1 DF2
a 1 1
b 1 1
c 1 0
d 1 0
x 0 1
y 0 1
You can use merge
with series.str.get_dummies()
together to achieve this:
m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)
d={'both':'DF1,DF2','left_only':'DF1','right_only':'DF2'}
m=m.assign(_merge=m._merge.map(d))
m[['Feature']].join(m._merge.str.get_dummies(','))
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1