I would like to extract/filter the rows of a dataframe that contains the strings on a list, in this case I am trying to use queries since they usually are fantastic for this job and very elegant in the code, I have tried:
my_list = ['red', 'blue', 'green', 'yellow']
df_new = df.query("`User Color` in @my_list")
I am looking for a function that works like in (if the string is contained)
My dataframe df looks kind of like this:
name id User Color Age
Luis 876 blue, green 35
Charles 12 blue, brown 34
Luna 654 black 24
Anna 987 brown 19
Silvana 31 red, black 26
Juliet 55 red 20
And the output I expect should be:
name id User Color Age
Luis 876 blue, green 35
Charles 12 blue, brown 34
Silvana 31 red, black 26
Juliet 55 red 20
df_subset = df[df['User Color'].map(lambda val: any(x in my_list for x in val.split(',')))]this should do the trickval.split(','): )