I have one pandas dataframe like this:
>>> df1
col_1 col_2 labels
1 aaa abc [71020]
2 bbb cde [77085]
3 ccc efg [36415]
4 ddd ghi [99213, 99287]
5 eee ijk [99233, 71020, 36415]
and another dataframe like this(I got this df using sklearn.preprocessing.MultiLabelBinarizer on labels
column of above df1
):
>>> df2
71020 77085 36415 99213 99287 99233
1 1 0 0 0 0 0
2 0 1 0 0 0 0
3 0 0 1 0 0 0
4 0 0 0 1 1 0
5 1 0 1 0 0 1
and I would now like to merge(or right join) df2
on df2
, with keys being values from labels
column and all the column names of df2
, like shown below:
col_1 col_2 labels 71020 77085 36415 99213 99287 99233
1 aaa abc [71020] 1 0 0 0 0 0
2 bbb def [77085] 0 1 0 0 0 0
3 ccc ghi [36415] 0 0 1 0 0 0
4 ddd jkl [99213, 99287] 0 0 0 1 1 0
5 eee mno [99233, 71020, 36415] 1 0 1 0 0 1
How do I do this?