0

I have a dataframe, where every row has one non zero value only. I want to read the column number of this non zero value and put it in a new column. this is the dataframe

  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0  0.0  0.0   
  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0  0.0  0.0   
  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0 ...   0.0  0.0  0.0   
  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0  0.0  0.0   
  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 ...   0.0  0.0  0.0   

The size of df is 30029x51. The column created should be

  2  
  2
  4 
  2
  2

Any idea on doing this using pandas

Ara
  • 145
  • 2
  • 10
  • `(df == 1).idxmax(axis=1)` - [Here](https://stackoverflow.com/questions/29919306/find-the-column-name-which-has-the-maximum-value-for-each-row/29919489)'s a duplicate. `df.idxmax(axis=1)` also works if you only have zeros and ones. – ayhan Apr 28 '18 at 16:46
  • Thank You , this is exactly what I am looking for – Ara Apr 28 '18 at 16:52

0 Answers0