I have an sample dataset.
raw_data = {
'categories': ['sweet beverage', 'salty snacks', 'beverage,sweet', 'fruit juice,beverage,', 'salty crackers'],
'product_name': ['coca-cola', 'salted pistachios', 'fruit juice', 'lemon tea', 'roasted peanuts']}
df_a = pd.DataFrame(raw_data)
I need to iterate thru the rows in the 'categories' columns, and check if it contains a particular string, in this case, 'beverage', after which i will update the categories to just 'beverage'. This link is the closest i found on stackoverflow, but doesnt tell me how to go thru the whole dataset.
Replace whole string if it contains substring in pandas
Here's my sample code.
for index,row in df.iterrows():
if row.str.contains('beverage', na=False):
df.loc[index,'categories_en'] = 'Beverages'
elif row.str.contains('salty',na=False):
df.loc[index,'categories_en'] = 'Salty Snack'
....<and other conditions>
How can I achive this? Thanks all!