Search code examples
pythonpandasstatisticsdummy-variable

How to transform multiple dummy variables to string


So I have data set like this:

    a_1 a_2 a_3
0   1   1   0
1   0   1   0
2   0   1   1
3   1   0   0

And I want to cut in to one column so it looks like it:

    a
0   1|2
1   2  
2   2|3
3   1  

This way I can then easily redo it back to dummy if I need it later.

So how to do it? pandas.from_dummies - works only for one choice cases so maybe there is a fast way to do it.


Solution

  • converting row data to list such [1 ,1 ,0] and find out the index number with 1, so output will be [0, 1] as you start index from 1, I have incremented the list elemnts with +1. last converted list to string using join fun with | character.

    Code:

    df.apply(lambda x: '|'.join([str(i+1) for i, e in enumerate(list(x)) if e == 1]),axis=1)
    

    Ouput:

    0    1|2
    1      2
    2    2|3
    3      1
    dtype: object