Skip to content
Advertisement

How to filter a dataframe column having multiple values in Python

I have a data frame that sometimes has multiple values in cells like this:

df:
Fruits
apple, pineapple, mango
guava, blueberry, apple
custard-apple, cranberry
banana, kiwi, peach
apple

Now, I want to filter the data frame having an apple in the value. So my output should look like this:

Fruits
apple, pineapple, mango
guava, blueberry, apple
apple

I used the str.contains(‘apple’) but this is not returning the ideal result.

Can anyone help me with how I can get this result?

Advertisement

Answer

You can split the data by ,, explode them, then compare with apple:

mask = df['Fruits'].str.split(', ').explode().eq('apple').groupby(level=0).any()
df[mask]

Output:

                    Fruits
0  apple, pineapple, mango
1  guava, blueberry, apple
4                    apple
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement