Golden
Golden

Reputation: 417

Pandas - Check if all elemets in a list columns are in another list

I have a dataframe which look like this:

   Name  Fruits                                                                                                                      
1  Bob  Apple,Banana
2  Sam  Apple,Orange
3  George Banana,Kiwi,Cherry

and another list that looks like this:

Allowed_fruits = [Apple,Banana]

I'm trying to write a test to check whatever all elements in the Fruits columns are in the allowed_fruits list.

If not - they should be inserted to another dataframe.

The output should be:

Found Not allowed Fruits !
  Name  Fruits                                                                                                                      
2  Sam  Orange
3  George Kiwi,Cherry

Any ideas ?

Upvotes: 1

Views: 81

Answers (1)

jezrael
jezrael

Reputation: 862911

Use DataFrame.assign for new DataFrame with test in generator comprehension with with splitted values:

Allowed_fruits = ['Apple','Banana']

f = lambda x: ','.join(y for y in x.split(',') if y not in Allowed_fruits)
df1 = df.assign(Fruits = df['Fruits'].apply(f))

Or:

L = [','.join(y for y in x.split(',') if y not in Allowed_fruits) for x in df['Fruits']]
df1 = df.assign(Fruits = L)

and last reove rows with no values, comparing with empty string:

df1 = df1[df1['Fruits'].ne('')]
print (df1)
     Name       Fruits
2     Sam       Orange
3  George  Kiwi,Cherry

Upvotes: 3

Related Questions