stream_entry
stream_entry

Reputation: 55

Get a frequency table for a column of lists

Suppose I have the DataFrame where I have a column of lists.

df = pd.DataFrame({'A': [['a', 'b', 'c'], ['b'], ['c'], ['a', 'b']]})

with the output

Index  A
0      ['a', 'b', 'c']
1      ['b']
2      ['c']
3      ['a', 'b']

How do I get a frequency table for how often a list appears in the column?

The ideal output would look like

A               Count
['a', 'b', 'c'] 1
['b']           1
['c']           1
['a', 'b']      1

Attempting something like this...

df.A.value_counts()

leads to the error

TypeError: unhashable type: 'list'

Upvotes: 1

Views: 190

Answers (1)

yatu
yatu

Reputation: 88226

map to tuples, lists are not hashable as the error suggests:

df.A.map(tuple).value_counts().rename_axis('A').reset_index(name='Count')

           A  Count
0  (a, b, c)      1
1     (a, b)      1
2       (b,)      1
3       (c,)      1

Upvotes: 2

Related Questions