Solving pd.get_dummies dysfunction in python

1.3k Views Asked by At

I have

 a={0: ['I3925'], 1: ['I3925'], 2: ['I3925'], 3: ['I2355'], 4: ['I2355'], 5: ['I2355'], 6: ['I111'], 7: ['I111'], 8: ['I111'], 9: ['I405'], 10: ['I405'], 11: ['I3878', 'I2864'], 12: ['I3878'], 13: ['I534'], 14: ['I534'], 15: ['I134', 'I2276'], 16: ['I107'], 17: ['I107'], 18: ['I2864']}  

which contains one supplementary I number for one key.

b = pd.Series(a,\
                              index = a.keys(),
                               name = "a")
pd.get_dummies(b.apply(pd.Series))

then get_dummies is not working, as it creates a duplicate column 1_15 to store the match with the second I number, instead of stacking them into the same column. I don't understand why.

    0_I107  0_I111  0_I134  0_I2355 0_I2864 0_I3878 0_I3925 0_I405  0_I534  1_I2276 1_I2864
0   0   0   0   0   0   0   1   0   0   0   0
1   0   0   0   0   0   0   1   0   0   0   0
2   0   0   0   0   0   0   1   0   0   0   0
3   0   0   0   1   0   0   0   0   0   0   0
4   0   0   0   1   0   0   0   0   0   0   0
5   0   0   0   1   0   0   0   0   0   0   0
6   0   1   0   0   0   0   0   0   0   0   0
7   0   1   0   0   0   0   0   0   0   0   0
8   0   1   0   0   0   0   0   0   0   0   0
9   0   0   0   0   0   0   0   1   0   0   0
10  0   0   0   0   0   0   0   1   0   0   0
11  0   0   0   0   0   1   0   0   0   0   1
12  0   0   0   0   0   1   0   0   0   0   0
13  0   0   0   0   0   0   0   0   1   0   0
14  0   0   0   0   0   0   0   0   1   0   0
15  0   0   1   0   0   0   0   0   0   1   0
16  1   0   0   0   0   0   0   0   0   0   0
17  1   0   0   0   0   0   0   0   0   0   0
18  0   0   0   0   1   0   0   0   0   0   0

Could someone please explain what I am doing wrong?

2

There are 2 best solutions below

2
On

Something like this?

pd.get_dummies(b.apply(pd.Series).stack()).sum(level=0)

Output:

    I107  I111  I134  I2276  I2355  I2864  I3878  I3925  I405  I534
0      0     0     0      0      0      0      0      1     0     0
1      0     0     0      0      0      0      0      1     0     0
2      0     0     0      0      0      0      0      1     0     0
3      0     0     0      0      1      0      0      0     0     0
4      0     0     0      0      1      0      0      0     0     0
5      0     0     0      0      1      0      0      0     0     0
6      0     1     0      0      0      0      0      0     0     0
7      0     1     0      0      0      0      0      0     0     0
8      0     1     0      0      0      0      0      0     0     0
9      0     0     0      0      0      0      0      0     1     0
10     0     0     0      0      0      0      0      0     1     0
11     0     0     0      0      0      1      1      0     0     0
12     0     0     0      0      0      0      1      0     0     0
13     0     0     0      0      0      0      0      0     0     1
14     0     0     0      0      0      0      0      0     0     1
15     0     0     1      1      0      0      0      0     0     0
16     1     0     0      0      0      0      0      0     0     0
17     1     0     0      0      0      0      0      0     0     0
18     0     0     0      0      0      1      0      0     0     0
2
On

Option 1

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
pd.DataFrame(mlb.fit_transform(b), b.index, mlb.classes_)

    I107  I111  I134  I2276  I2355  I2864  I3878  I3925  I405  I534
0      0     0     0      0      0      0      0      1     0     0
1      0     0     0      0      0      0      0      1     0     0
2      0     0     0      0      0      0      0      1     0     0
3      0     0     0      0      1      0      0      0     0     0
4      0     0     0      0      1      0      0      0     0     0
5      0     0     0      0      1      0      0      0     0     0
6      0     1     0      0      0      0      0      0     0     0
7      0     1     0      0      0      0      0      0     0     0
8      0     1     0      0      0      0      0      0     0     0
9      0     0     0      0      0      0      0      0     1     0
10     0     0     0      0      0      0      0      0     1     0
11     0     0     0      0      0      1      1      0     0     0
12     0     0     0      0      0      0      1      0     0     0
13     0     0     0      0      0      0      0      0     0     1
14     0     0     0      0      0      0      0      0     0     1
15     0     0     1      1      0      0      0      0     0     0
16     1     0     0      0      0      0      0      0     0     0
17     1     0     0      0      0      0      0      0     0     0
18     0     0     0      0      0      1      0      0     0     0

Option 2

b.str.join('|').str.get_dummies()

    I107  I111  I134  I2276  I2355  I2864  I3878  I3925  I405  I534
0      0     0     0      0      0      0      0      1     0     0
1      0     0     0      0      0      0      0      1     0     0
2      0     0     0      0      0      0      0      1     0     0
3      0     0     0      0      1      0      0      0     0     0
4      0     0     0      0      1      0      0      0     0     0
5      0     0     0      0      1      0      0      0     0     0
6      0     1     0      0      0      0      0      0     0     0
7      0     1     0      0      0      0      0      0     0     0
8      0     1     0      0      0      0      0      0     0     0
9      0     0     0      0      0      0      0      0     1     0
10     0     0     0      0      0      0      0      0     1     0
11     0     0     0      0      0      1      1      0     0     0
12     0     0     0      0      0      0      1      0     0     0
13     0     0     0      0      0      0      0      0     0     1
14     0     0     0      0      0      0      0      0     0     1
15     0     0     1      1      0      0      0      0     0     0
16     1     0     0      0      0      0      0      0     0     0
17     1     0     0      0      0      0      0      0     0     0
18     0     0     0      0      0      1      0      0     0     0