Search code examples
pythonpandasbinarydummy-variable

How to turn a DataFrame column of binary variables into multiple columns of dummy variables


This should be an easy question, but for some reason I can't find the answer online. I have a DataFrame column that consists of dummy variables:

import pandas as pd

foo = pd.Series([6,7,8,3])
foo1 = bob.apply(lambda x: bin(x)[2:].zfill(4))
foo1

0    0110
1    0111
2    1000
3    0011

What I want is a 4x4 data frame that looks like

A B C D
0 1 1 0
0 1 1 1
1 0 0 0
0 0 1 1

I've tried using get_dummies to no results:

foo1.str.get_dummies()

0110 0111 1000 0011
1    0    0    0
0    1    0    0
0    0    1    0
0    0    0    1

str.split and making the column into a series of lists doesn't work either. What should I do?


Solution

  • You can try this:

    # convert the series to str type; 
    # extract all characters with regex .; 
    # unstack to wide format
    foo1.astype(str).str.extractall('(.)')[0].unstack()
    

    enter image description here