First, I have looked at many SO threads on this and none seemed to work in make case. Creating a new column based on if-elif-else condition seemed to be the closest to what I am trying to do.
In my df I have a column with product names. I am trying to create a function that looks for a partial string match in each row of that column and based on the match it will create a label for each row in a new df column. I wanted to use a function because there are about 5 or 6 patterns that I need to match.
I am using contains() function to look for partial product title match. This returns a bool which I then check with else/if in the function:
def label_sub_cat():
if data['product'].str.contains('Proceedings', case=False) is True:
return 'Proceedings'
elif data['product'].str.contains('DVD', case=False) is True:
return 'DVD'
else:
return 'Other'
data['product_sub_cat'] = data.apply(label_sub_cat(), axis=1)
I keep getting the following error:
AttributeError: 'DataFrame' object has no attribute 'other'