LuchoSEO
LuchoSEO

Reputation: 31

Create new column based on a keyword match

Suppose I have this DF:

df = pd.DataFrame({'keyword': ['Rayban', 'Rayban lens', 'Sunglasses RayBan', 'Sunglasses'],'Volume':[50,100,150,200]})

             keyword  Volume
0             Rayban      50
1        Rayban lens     100
2  Sunglasses RayBan     150
3         Sunglasses     200

I need to create a new column "Brand" based on a keyword match. If the row on the column Keyword contains the string "Rayban", I need a that the row in the "Brand" column gets the value "Brand".

How could I do this ?

Based on the above example, I would need this output :

             keyword  Volume  Brand
0             Rayban      50  brand
1        Rayban lens     100  brand
2  Sunglasses RayBan     150  brand
3         Sunglasses     200  non-brand

I'm a beginner so let me know if something is not clear. Thank you for your help !

Upvotes: 0

Views: 126

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24304

try via str.contains():

df['Brand']='non-brand'
df.loc[df['keyword'].str.contains('Rayban',case=False),'Brand']='brand'

OR

import numpy as np

df['Brand']=np.where(df['keyword'].str.contains('Rayban',case=False),'brand','non-brand')

output of df:

    keyword         Volume      Brand
0   Rayban              50      brand
1   Rayban lens         100     brand
2   Sunglasses RayBan   150     brand
3   Sunglasses          200     non-brand

Upvotes: 1

Related Questions