I have a column having over 800 rows shown below:
0 ['Overgrow', 'Chlorophyll']
1 ['Overgrow', 'Chlorophyll']
2 ['Overgrow', 'Chlorophyll']
3 ['Blaze', 'Solar Power']
4 ['Blaze', 'Solar Power']
5 ['Blaze', 'Solar Power']
6 ['Torrent', 'Rain Dish']
7 ['Torrent', 'Rain Dish']
8 ['Torrent', 'Rain Dish']
9 ['Shield Dust', 'Run Away']
10 ['Shed Skin']
11 ['Compoundeyes', 'Tinted Lens']
12 ['Shield Dust', 'Run Away']
13 ['Shed Skin']
14 ['Swarm', 'Sniper']
15 ['Keen Eye', 'Tangled Feet', 'Big Pecks']
16 ['Keen Eye', 'Tangled Feet', 'Big Pecks']
17 ['Keen Eye', 'Tangled Feet', 'Big Pecks']
What do I want?
- I would like to count the number of times each string value has occurred.
- I also would like to arrange the unique string values into a list.
Here is what I have done to obtain the second part:
list_ability = df_pokemon['abilities'].tolist()
new_list = []
for i in range(0, len(list_ability)):
m = re.findall(r"'(.*?)'", list_ability[i], re.DOTALL)
for j in range(0, len(m)):
new_list.append(m[j])
list1 = set(new_list)
I am able to get the unique string values into a list, but is there a better way?
Example:
'Overgrow' - 3
'Chlorophyll' - 3
'Blaze' - 3
'Sheild Dust' - 2 .... and so on
(By the way, the name of the column is 'abilities' from the dataframe df_pokemon.)
from collections import Counter; counts = df_pokemon.abilities.map(Counter).sum()?