I've been having a bit of trouble with this problem. I have a data frame of clothing items which have an associated description as below:
Item Description
R2G1 RED, BLUE, SHIRT
G23A YELLOW SHIRT
P001 BLUE, PINK SKIRT
I also have a list of items that contain the possible categories of the clothing items, ie.
categories = ['RED', 'BLUE', 'YELLOW', 'PINK', 'SHIRT', 'SKIRT']
I need to check the description fields of each item to see if they contain any of the strings in the categories list, and assign them the correct binary values in new columns based on the categories. The final output should look like this:
Item Description Red Blue Yellow Pink Shirt Skirt
R2G1 RED, BLUE, SHIRT 1 1 0 0 1 0
G23A YELLOW SHIRT 0 0 1 0 1 0
P001 BLUE, PINK SKIRT 0 1 0 1 0 1
I've tried to use this function but I keep getting the AttributeError: 'float' object has no attribute 'upper' error when I try to use it as follows:
def get_category(series):
res = []
for i in category_list:
if i in series.upper():
res.append(i)
return res
df['Categories'] = df['Description'].apply(get_model)
df = df.join(df['Model'].str.join('|').str.get_dummies())