I would like to reverse a dataframe with dummy variables. For example,
from df_input:
JavaScript
x
5
1
Course_01 Course_02 Course_03
2
0 0 1
3
1 0 0
4
0 1 0
5
To df_output
JavaScript
1
5
1
Course
2
0 03
3
1 01
4
2 02
5
I have been looking at the solution provided at Reconstruct a categorical variable from dummies in pandas but it did not work. Please, Any help would be much appreciated.
Many Thanks, Best Regards, Carlo
Advertisement
Answer
We can use wide_to_long
, then select rows that are not equal to zero i.e
JavaScript
1
15
15
1
ndf = pd.wide_to_long(df, stubnames='T_', i='id',j='T')
2
3
T_
4
id T
5
id1 30 0
6
id2 30 1
7
id1 40 1
8
id2 40 0
9
10
not_dummy = ndf[ndf['T_'].ne(0)].reset_index().drop('T_',1)
11
12
id T
13
0 id2 30
14
1 id1 40
15
Update based on your edit :
JavaScript
1
9
1
ndf = pd.wide_to_long(df.reset_index(), stubnames='T_',i='index',j='T')
2
3
not_dummy = ndf[ndf['T_'].ne(0)].reset_index(level='T').drop('T_',1)
4
5
T
6
index
7
1 30
8
0 40
9