Say I have a DataFrame pd with a column called 'elements' which contains a list of a list of objects as shown below:
print(df2['elements'])
0 [Element B, Element Cr, Element Re]
1 [Element B, Element Rh, Element Sc]
2 [Element B, Element Mo, Element Y]
3 [Element Al, Element B, Element Lu]
4 [Element B, Element Dy, Element Os]
Name: elements, Length: 1763, dtype: object
I would like to count how many times each string within the whole column, so like in the example above the count for the string 'Element B' is 5 or for 'Element Mo' is 1.
I have tried setting up a dictionary below, but this just counts the each list instead of the strings that are in them.
elements_count_dict = {}
for entry in df2['elements']:
for object in entry:
if object in elements_count_dict:
elements_count_dict[object] += 1
else:
elements_count_dict[object] = 0
However, doing it this way the dictionary of tracks each individual character instead of the strings i.e. [ = 5 and ] = 5 and even after converting the column to string using df2['elements']to_string() it still doesn't work.