Initial post, apologizing in advance if my formatting is disabled.
Here is my problem:
I created a Pandas framework that contains several lines of text:
d = {'keywords' :['cheap shoes', 'luxury shoes', 'cheap hiking shoes']}
keywords = pd.DataFrame(d,columns=['keywords'])
In [7]: keywords
Out[7]:
keywords
0 cheap shoes
1 luxury shoes
2 cheap hiking shoes
Now I have a dictionary that contains the following keys / values:
labels = {'cheap' : 'budget', 'luxury' : 'expensive', 'hiking' : 'sport'}
What I would like to do is find out if the key exists in the dictionary in the data frame, and if so, return the appropriate value
I was able to refine some using the following:
for k,v in labels.items():
keywords['Labels'] = np.where(keywords['keywords'].str.contains(k),v,'No Match')
However, the output is missing the first two keys and it catches only the last "pedestrian" key
keywords Labels
0 cheap shoes No Match
1 luxury shoes No Match
2 cheap hiking shoes sport
In addition, I would also like to know if there is a way to catch multiple values in the dictionary, separated by the | character, so the ideal output would look like this:
keywords Labels
0 cheap shoes budget
1 luxury shoes expensive
2 cheap hiking shoes budget | sport
.