I have dataframe with movie titles and columns with genres. Such as movie with title 'One' is 'Action' and 'Vestern', because have '1' in appropriate columns.
Movie Action Fantasy Vestern
0 One 1 0 1
1 Two 0 0 1
2 Three 1 1 0
My goal is create column genres, which will contain name of each genres, that particular movie have.
For this I am tried used lambda and list comprehension, because thought this helps. But after runned such line of code as:
df['genres'] = df.apply(lambda x: [x+"|"+x for x in df.columns if x!=0])
I got only NaN value in each row:
Movie Action Fantasy Vestern genres
0 One 1 0 1 NaN
1 Two 0 0 1 NaN
2 Three 1 1 0 NaN
Also tried to use groupby, but didn't succeed.
Expected output is:
Movie Action Fantasy Vestern genres
0 One 1 0 1 Action|Vestern
1 Two 0 0 1 Vestern
2 Three 1 1 0 Action|Fantasy
Code to reproduce:
import pandas as pd
import numpy as np
df = pd.DataFrame({"Movie":['One','Two','Three'],
"Action":[1,0,1],
"Fantasy":[0,0,1],
"Vestern":[1,1,0]})
print(df)
Thanks for your help