Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert one-hot encoded data-frame columns into one column

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.

like image 246
Eisha Tir Raazia Avatar asked Jul 31 '20 17:07

Eisha Tir Raazia


People also ask

How do you one-hot encode the column?

For basic one-hot encoding with Pandas you pass your data frame into the get_dummies function. This returns a new dataframe with a column for every "level" of rating that exists, along with either a 1 or 0 specifying the presence of that rating for a given observation.

What is OneHotEncoder in Python?

OneHotEncoder. Encode categorical integer features using a one-hot aka one-of-K scheme. The input to this transformer should be a matrix of integers, denoting the values taken on by categorical (discrete) features. The output will be a sparse matrix where each column corresponds to one possible value of one feature.

How do I get data frames from a specific column?

Selecting columns based on their name This is the most basic way to select a single column from a dataframe, just put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.


3 Answers

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
like image 196
BENY Avatar answered Sep 22 '22 14:09

BENY


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
like image 20
anky Avatar answered Sep 21 '22 14:09

anky


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
like image 41
ALollz Avatar answered Sep 22 '22 14:09

ALollz