0

Here is what I have

>>> s = pd.Series([0,2,1,0], ['t1','t2','t3','t4'])
>>> s
t1    0
t2    2
t3    1
t4    0
dtype: int64

I want an output which looks like this with binary indicators for each value as pandas dataframe:

value  t1   t2   t3   t4
    0   1    0    0    1
    1   0    0    1    0
    2   0    1    0    0

1 Answers1

2

Looks like you need get_dummies:

pd.get_dummies(s)

#    0  1  2
#t1  1  0  0
#t2  0  0  1
#t3  0  1  0
#t4  1  0  0

You can further transpose it if you need it in the other way:

pd.get_dummies(s).T

#   t1  t2  t3  t4
#0   1   0   0   1
#1   0   0   1   0
#2   0   1   0   0
Psidom
  • 209,562
  • 33
  • 339
  • 356