I would like to loop over each row of a data frame and if there's a match between a column and a string from a list I would add an element in a new column. In this example I want to add a new column to categorize the products.. so if a row of the column match one of the lists, the category could be either 'Drinks' or 'Food' and if there's no match the category would be other.
list_drinks={'Water','Juice','Tea'}
list_food={'Apple','Orange'}
data = {'Price': ['1', '5','3'], 'Product': ['Juice','book', Pen]}
for (i,j) in itertools.zip_longest(list_drinks,list_food):
for index in data.index:
if(j in data.loc[index,'product']):
data["Category"] = "Food"
elif(i in data.loc[index,'product']):
data["Category"] ="drinks"
else:
data["Category"]="Other"
The output would be :
Price Product Category
1 Juice drinks
5 book Other
3 Pen Other
My problem is mainly I don't know how to match the patterns between the lists and the rows. I tried also:
str.contains but it did not work.