Rods2292
Rods2292

Reputation: 675

Python - One-hot-encode to single column

I'm almost zero experienced with python but I'm trying to learn it. I have a Pandas dataframe which came with some dummies. I want to convert them back to a single column but I simply can't figure out a way to that. Is there any way to that?

I have this:

ID   var_1 var_2 var_3 var_4
231    1     0     0    0
220    0     1     0    0
303    0     0     1    0
324    0     0     0    1

I need to transform to it:

ID   var  
231    1   
220    2   
303    3    
324    4

Upvotes: 1

Views: 620

Answers (2)

BENY
BENY

Reputation: 323226

Try something new wide_to_long

s=pd.wide_to_long(df,['var'],i='ID',j='Var',sep='_')
s[s['var']==1].reset_index().drop('var',1)
Out[593]: 
    ID Var
0  231   1
1  220   2
2  303   3
3  324   4

Upvotes: 1

cs95
cs95

Reputation: 402253

Assuming these really are one-hot-encodings, use np.argmax along the first axis:

pd.DataFrame({'ID' : df['ID'], 'var' : df.iloc[:, 1:].values.argmax(axis=1) + 1})

    ID  var
0  231    1
1  220    2
2  303    3
3  324    4

However, if "ID" is a part of the index, use this instead:

pd.DataFrame({'ID' : df.index, 'var' : df.values.argmax(axis=1)})

Upvotes: 1

Related Questions