I'm trying to create a new column in a pandas dataframe based if a column contains a partial string.
I have a simple dataframe
Food Price
0 apple 1.00
1 banana 2.99
2 carrot 3.50
and I would like to append a column to it like so
Food Price Sale Status
0 apple 1.00 on sale
1 banana 2.99 not on sale
2 carrot 3.50 on sale next week
This work if my keys are exact matches to the values in the Food column
my_dict={'apple':'on sale', 'banana':'not on sale', 'carrot':'on sale next week'}
df['Sale Status']= df['Food'].map(my_dict)
My problem is that my dataframe looks like this
Food Price
0 some other words apple 1.00
1 other banana text 2.99
2 blah blah carrot 3.50
Is there a way to match the dictionary values if the sting in Food contains the key name?
CodePudding user response:
There are several solutions. You can iterate over 'Food' and map the key to its value if found, like
df['Sale Status'] = df['Food'].apply(lambda x: [v for k, v in my_dict.items() if k in x][0])
Or you can use str.extract()
df['Sale Status'] = df['Food'].str.extract('(' '|'.join(my_dict.keys()) ')', expand=False).map(my_dict)