I am trying to create a new column based on the following logic by using the function below. When I apply this function to a df using lambda, I am seeing the following error. I tried to remove the "str" in front of "contains" but it did not work. Could anyone assist or advise? Thanks.
def new_col(x):
if pd.isna(x):
return ''
elif x.str.contains('Watch',case=False):
return 'Product A'
elif x.str.contains('Glasses',case=False):
return 'Product B'
elif x.str.contians('Table',case=False):
return 'Product C'
elif x.str.contains('Computer',case=False):
return 'Product D'
elif x.str.contains('Beauty',case=False):
return 'Product E'
elif x.str.contains(','):
return x.split(',')[0]
else:
return x
df['new column'] = df.apply(lambda x: new_col(x['product']),axis=1)
AttributeError: 'str' object has no attribute 'contains'