title: How to use pandas string contains (str.contain) in a loop?
body: <p>I have a pandas df with a column A, which is a string of strings. Each item in the series (i.e. each row in the database) is just one long string, separated by commas. I would like to create a new column called B that incrementally counts everytime an object from a separate list appears in each row of column A. For instance:</p>
  
  <p>my list looks like this:</p>
  
  <pre><code>list = ('dog', 'bird', 'cat')
  </code></pre>
  
  <p>my dataframe looks like this:</p>
  
  <pre><code>A                           B
  dog, bird                   2
  cat, bird                   2
  dog, snake                  1
  cat, bird, snake            2
  dog, bird, cat, snake       3
  dog, bird cat               3
  </code></pre>
  
  <p>i'm trying to create a nested loop that does the following: start at df.a[0] (i.e. the first value of df.A), find out if it contains the first value of the list (i.e. 'dog'). If df.A[0] contains, then add 1 to B. then, staying in the same row of df.A, move on to the second value of the list (i.e. 'bird'). If that df.A[0] also contains this value, then add another 1 to B. etc etc.</p>
  
  <p>This is the code I'm trying to use. </p>
  
  <pre><code>for i in df['A']:
      for j in list:
          if i.str.contains(j):
              df['B'] += 1
  </code></pre>
  
  <p>However, I keep getting the error:</p>
  
  <pre><code>'str' object has no attribute 'str'
  </code></pre>
  
  <p>How can I tell pandas to look at the entire series, while also telling it to function as a loop with the structure identified above? Or alternatively, what is the best way to solve this problem?</p>