title: How do I compare a string to a string in a dataframe using pandas?
body: <p>Let's say I have a string stored in <code>text</code>. I want to compare this string with a list of strings stored in a dataframe and check if the <code>text</code> contains words like car, plane, etc. For each keyword found, I want to add 1 value belonging to the correlated topic. </p>
  
  <pre><code>| topic      | keywords                                  |
  |------------|-------------------------------------------|
  | Vehicles   | [car, plane, motorcycle, bus]             |
  | Electronic | [television, radio, computer, smartphone] |
  | Fruits     | [apple, orange, grape]                    |
  </code></pre>
  
  <p>I have written the following code, but I don't really like it. And it doesn't work as intended.</p>
  
  <pre><code>def foo(text, df_lex):
  
      keyword = []
      score = []
      for lex_list in df_lex['keyword']:
          print(lex_list)
          val = 0
          for lex in lex_list:
  
              if lex in text:
                  val =+ 1
          keyword.append(key)
          score.append(val)
      score_list = pd.DataFrame({
      'keyword':keyword,
      'score':score
      })
  </code></pre>
  
  <p>Is there a way to do this efficiently? I don't like having too many loopings in my program, as they don't seem to be very efficient. I will elaborate more if needed. Thank you.</p>
  
  <p>EDIT: For example my text is like this. I made it simple, just so it's understandable.</p>
  
  <p>I went to the showroom riding a <code>motorcycle</code> to buy a <code>car</code> today. Unluckily, when I checked my <code>smartphone</code>, I got a message to go home.</p>
  
  <p>So, my expected output would be something like this.</p>
  
  <pre><code>| topic      | score |
  |------------|-------|
  | Vehicles   | 2     |
  | Electronic | 1     |
  | Fruits     | 0     |
  </code></pre>
  
  <p>EDIT2: I finally found my own solution with some help from @jezrael.</p>
  
  <pre><code>df['keywords'] = df['keywords'].str.strip('[]').str.split(', ')
  
  text = 'I went to the showroom riding a motorcycle to buy a car today. Unluckily, when I checked my smartphone, I got a message to go home.'
  
  score_list = []
  for lex in df['keywords']:
      val = 0
      for w in lex:
          if w in text:
              val +=1
      score_list.append(val)
  df['score'] = score_list
  print(df)
  </code></pre>
  
  <p>And it prints exactly what I need.</p>