'Get only those rows from a pandas data frame where a particular value is not present in a data frame column of type list

One of my columns in dataframe is of type list.

enter image description here

I want to get only those rows where color is not red.Output should give row 2 and 3. I tried

    def Removecolor(red):
        x = pd.DataFrame([['a',"image1",['red',"white"]],['b',"image2", 
        ['red',"blue"]],['c',"image3",['black',"white"]],
                 ['d',"image3",[]]],columns=["id","image","color"])
        temp = x.color.apply(lambda row: any(item for item in red if item in row))
    final_color = x[~temp]

    return (final_color)

But the output I got is same dataframe



Solution 1:[1]

Problem is that you are looping 'red' word rather than the list in item for item in red if item in row. You can use

m = df.color.apply(lambda lst: 'red' in lst)
print(m)

0     True
1     True
2    False
3    False
Name: color, dtype: bool
# Use `not in` can help remove `~` here
final_dishes = df[~m]
print(final_dishes)

    image           color
2  image3  [black, white]
3  image3              []

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynjxsjmh