Consider the following DataFrame:
df = pd.DataFrame({'animals': [['dog','cat','snake','lion','tiger'],
['dog','moose','alligator','lion','tiger'],
['eagle','moose','alligator','lion','tiger'],
['cat','alligator','lion']]})
I need to extract every single unique animal and sum the number of occurrences. The output should be something like:
dog 2
cat 2
snake 1
lion 4
tiger 3
moose 2
alligator 3
eagle 1
Similar to what df.value_counts() does.
Much appreciated.
You can use explode
and value_counts
:
df.animals.explode().value_counts()
Output:
lion 4
tiger 3
alligator 3
moose 2
cat 2
dog 2
eagle 1
snake 1
Name: animals, dtype: int64
One way with Counter
+ chain
import pandas as pd
from collections import Counter
from itertools import chain
pd.Series(Counter(chain.from_iterable(df['animals'])))
dog 2
cat 2
snake 1
lion 4
tiger 3
moose 2
alligator 3
eagle 1
dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With