2

I have data as shown below:

enter image description here

I have a reference table shown below:

enter image description here

I want to loop through data table's "Description" to see if the string contains each word in reference table "Key" column, if so, I will enter corresponding category in reference table's column "Category" to data table's "Category" column. Both tables are Dataframe.

I am able to assign value to the data based on one key:

k = 'apple'
ind=df[df['Description'].str.contains(k)].index
df.loc[ind,'Category'] = 'A'

If I want to loop through the reference table "Key" column, should I convert the reference table to dictionary or just set key column as index? How should I loop it?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
emma12345
  • 107
  • 4
  • 10
  • 1
    Does this answer your question? [Python Pandas - Merge based on substring in string](https://stackoverflow.com/questions/48743662/python-pandas-merge-based-on-substring-in-string) – G. Anderson Feb 11 '20 at 21:20
  • Can you please give a sample dataframe? – XXavier Feb 11 '20 at 21:20
  • Hi G.Anderson, awesome codes, however, I have a problem with my data, in description, the sentence can contain 2 or more words. For example, it can be "I went to dance and got honey", so i want to loop through it and make the later loop correct the prior one – emma12345 Feb 11 '20 at 21:43

0 Answers0