st19297 st19297 - 13 days ago 5
Python Question

Counting occurrence of values in a Panda series?

I have a panda series

l=pd.Series([3, 1, 4, 2, [1, 2, 10]])


I need to get something like:

value count
3 1
1 2
4 1
2 2
10 1

l.value_counts()


gives me:

TypeError: unhashable type: 'list'


I even tried to flatten the list like this:

chain = itertools.chain(*l)
print(list(chain))


But it gives me:

TypeError: 'list' object is not callable

Answer

If your data size is not very large, you can use this work around:

l.apply(pd.Series).stack().value_counts()

#2.0     2
#1.0     2
#10.0    1
#4.0     1
#3.0     1
#dtype: int64

Or another option with chain:

from itertools import chain
pd.Series(list(chain.from_iterable(i if isinstance(i, list) else [i] for i in l))).value_counts()

#2     2
#1     2
#10    1
#4     1
#3     1
#dtype: int64

And also can use Counter from collections:

from itertools import chain
from collections import Counter
pd.Series(Counter(chain.from_iterable(i if isinstance(i, list) else [i] for i in l)))

#2     2
#1     2
#10    1
#4     1
#3     1
#dtype: int64