'Iterate over lists within a column to create a new column

I have a dataset listing an item's category and the interests of a customer. I'd like to add a third column using python to identify whether the item category falls under the customer's interests.

Current Setup:

Category Interests
Baseball ['Baseball', 'Dancing']
Racing ['Video Games', 'Music']

Goal:

Category Interests In_Interests
Baseball ['Baseball', 'Dancing'] Yes
Racing ['Video Games', 'Music'] No

So, I'd like to iterate over the lists in the interests column and use this to fill the entries in the third column.

I've tried setting code up similar to this:

df['In_Interests'] = np.where(df['Category'].isin(df.Interests), 'Yes', 'No')

But when I do this, the new column only fills with 'No' values.

Note: as it stands right now, the data type for both of these columns is "object". I'm not sure if this needs to be changed or not

Any assistance would be appreciated. Thank you!



Solution 1:[1]

I believe this will solve your issue :

df['In_Interests'] = df.apply(lambda row : np.where(row['Category'] in row['Interests'], 'Yes', 'No'), axis = 1)

print(df)

# Output
#   category   Interests           In_Interests
#0  Baseball   [Baseball, Dancing]          Yes
#1    Racing   [Video Games, Music]          No

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 gustavolq