0

I have a column of product names and I want to extract brand names for the same from my list.

my_list=['Lakme','Olay','Estee Lauder']

The column df['product_name'] has product names like 'Lakme Absolute Blur Perfect Makeup Primer'.

I want to create a new column df['brand'] if the brand name lies in the string from my_list.

I tried

df['brand']=df['product_name'].apply(lambda x: True if any(s in x for s in my_list) else False)

This just gives True/False. How do I extract brand in one step?

AmanArora
  • 2,379
  • 6
  • 19
  • 22
  • I found the solution. https://stackoverflow.com/questions/57948850/return-all-substrings-from-a-list-of-strings-in-a-dataframe-column This works – AmanArora Aug 11 '22 at 07:40
  • Is `my_list` long? If not, you could do `df['brand'] = df['product_name'].where(df['product_name'].str.contains('|'.join(my_list)))`. – Timus Aug 11 '22 at 08:42

0 Answers0