I have the following dataframe.
data = [[0, 0, 0, 0, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 0, 0, 0]]
labels = ['cat', 'dog', 'duck', 'fish', 'horse']
df = pd.DataFrame(data, columns = labels)
df:
cat dog duck fish horse
0 0 0 0 0 1
1 0 1 0 0 1
2 1 1 0 0 0
3 0 1 0 0 0
I have got the 0's and 1's from another dataframe based on a certain condition. I want to combine the column names for values corresponding to true values. i.e 1's into a list and put it into a new column at the end of the dataframe.
I want my result to look like this.
cat dog duck fish horse result
0 0 0 0 0 1 [horse]
1 0 1 0 0 1 [dog, horse]
2 1 1 0 0 0 [cat, dog]
3 0 1 0 0 0 [dog]
I have about 108 columns in my original dataframe and around 3500 rows. What is the best way to do this?
PS: I haven't been successful in finding a way to do this.
Thanks