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:
my list looks like this:
list = ('dog', 'bird', 'cat')
my dataframe looks like this:
A B
dog, bird 2
cat, bird 2
dog, snake 1
cat, bird, snake 2
dog, bird, cat, snake 3
dog, bird cat 3
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.
This is the code I'm trying to use.
for i in df['A']:
for j in list:
if i.str.contains(j):
df['B'] += 1
However, I keep getting the error:
'str' object has no attribute 'str'
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?