9

In the pandas data frame, the one-hot encoded vectors are present as columns, i.e:

Rows   A  B  C  D  E

0      0  0  0  1  0
1      0  0  1  0  0
2      0  1  0  0  0
3      0  0  0  1  0
4      1  0  0  0  0
4      0  0  0  0  1

How to convert these columns into one data frame column by label encoding them in python? i.e:

Rows   A  

0      4 
1      3  
2      2 
3      4 
4      1  
5      5  

Also need suggestion on this that some rows have multiple 1s, how to handle those rows because we can have only one category at a time.

Eisha Tir Raazia
  • 327
  • 3
  • 11

4 Answers4

6

Try with argmax

#df=df.set_index('Rows')

df['New']=df.values.argmax(1)+1
df
Out[231]: 
      A  B  C  D  E  New
Rows                    
0     0  0  0  1  0    4
1     0  0  1  0  0    3
2     0  1  0  0  0    2
3     0  0  0  1  0    4
4     1  0  0  0  0    1
4     0  0  0  0  1    5
BENY
  • 317,841
  • 20
  • 164
  • 234
6

argmaxis the way to go, adding another way using idxmax and get_indexer:

df['New'] = df.columns.get_indexer(df.idxmax(1))+1
#df.idxmax(1).map(df.columns.get_loc)+1
print(df)

Rows  A  B  C  D  E   New
                    
0     0  0  0  1  0    4
1     0  0  1  0  0    3
2     0  1  0  0  0    2
3     0  0  0  1  0    4
4     1  0  0  0  0    1
5     0  0  0  0  1    5
Eisha Tir Raazia
  • 327
  • 3
  • 11
anky
  • 74,114
  • 11
  • 41
  • 70
3

Also need suggestion on this that some rows have multiple 1s, how to handle those rows because we can have only one category at a time.

In this case you dot your DataFrame of dummies with an array of all the powers of 2 (based on the number of columns). This ensures that the presence of any unique combination of dummies (A, A+B, A+B+C, B+C, ...) will have a unique category label. (Added a few rows at the bottom to illustrate the unique counting)

df['Category'] = df.dot(2**np.arange(df.shape[1]))

      A  B  C  D  E  Category
Rows                         
0     0  0  0  1  0         8
1     0  0  1  0  0         4
2     0  1  0  0  0         2
3     0  0  0  1  0         8
4     1  0  0  0  0         1
5     0  0  0  0  1        16
6     1  0  0  0  1        17
7     0  1  0  0  1        18
8     1  1  0  0  1        19
Eisha Tir Raazia
  • 327
  • 3
  • 11
ALollz
  • 57,915
  • 7
  • 66
  • 89
  • Thanks, can you also tell the syntax if I have other float columns i.e: X, A...D, F, G, H too in the data-frame? – Eisha Tir Raazia Jul 31 '20 at 18:40
  • 1
    @EishaMazhar You won't need to change anything if those are all dummy columns. Otherwise I'd make a list of all of your dummy columns `dummy_cols = ['X', 'A', 'B', ...]` then you can do `df[dummy_cols].dot(2**np.arange(len(dummy_cols))` – ALollz Jul 31 '20 at 19:15
3

Another readable solution on top of other great solutions provided that works for ANY type of variables in your dataframe:

df['variables'] = np.where(df.values)[1]+1

output:

   A  B  C  D  E  variables
0  0  0  0  1  0          4
1  0  0  1  0  0          3
2  0  1  0  0  0          2
3  0  0  0  1  0          4
4  1  0  0  0  0          1
5  0  0  0  0  1          5
Ehsan
  • 12,072
  • 2
  • 20
  • 33