I have a set of columns that are either 1 or 0 depending on if a condition is true. Each row will have only a single column that is one.
I'm trying to add a new column to the df that finds the column number where the value equals 1 (e.g., column 0, 1, 2, etc.)
and return the corresponding indexed value in a list that has the same number of column elements as the dataframe.
For example, if the value is 1
in the second column for row 1
, I would want the newly added column to read b
using the list below as a reference.
prim_cols = ['a','b','c','d','e','f']
What is the best python functions to use for this? I've tried np.where, list comprehension, and a few others, but I'm stuck.