Merging two data frames into a new one with unique items marked with 1 or 0

Viewed 1189

I have several dataframes.

Dataframe #1

Feature   Coeff
a         0.5
b         0.3
c         0.35
d         0.2

Dataframe #2

Feature   Coeff
a         0.7
b         0.2
y         0.75
x         0.1

I want to merge this dataframe and obtain the following one:

Feature |  DF1  |  DF2
a          1       1
b          1       1
c          1       0
d          1       0
y          0       1
x          0       1

I know that I can do an outer merge but I do not know how to move from there to obtain the final dataframe I presented above. Any ideas?

4 Answers

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

Same Idea like user3483203 but with crosstab

u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])


pd.crosstab(u.Feature, u.index.get_level_values(0))

I merged two dataframes using pd.merge and used list comprehension to assign values.

df = df1.merge(df2, on='Feature', how='outer')
df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)

    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

I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies() if you can achieve the same using built-in methods/functions? Is it much faster? Genuinely curious since I'm a newbie myself.

(sorry I need more reputation points to leave comments directly under other answers!)

Related