3

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
st19297
  • 521
  • 1
  • 5
  • 18

3 Answers3

5

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
Psidom
  • 209,562
  • 33
  • 339
  • 356
1

Try

pd.value_counts([i for i in chain.from_iterable(l.values.tolist())])
piRSquared
  • 285,575
  • 57
  • 475
  • 624
1

Here is yet another solution, which uses np.hstack() and pd.value_counts() methods:

In [24]: pd.value_counts(np.hstack(l.values))
Out[24]:
2     2
1     2
10    1
4     1
3     1
dtype: int64
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419