Error unhashable type: 'list' when counting elements have a format list on visual code

Viewed 77

I have a dataframe with the elements of the format list (Number column). When I used a Jupiter notebook count elements, I successfully ran the code below, but when I converted the code to the py file using Visual Code, I got an error and could not run. Please help me.

Dataframe :

Numbers              Count_l
0   [3, 5, 8, 10, 29]   1
1   [3, 5, 8, 10, 30]   1
2   [3, 5, 8, 10, 33]   1
3   [3, 5, 8, 10, 38]   1
4   [3, 5, 8, 10, 47]   1

Jupiter notebook code :

dftemp = df.groupby("Numbers").Count_l.sum().sort_values(ascending = False )

And result :

[2, 4, 11, 63, 71]      45
[4, 12, 24, 71, 74]     38
[4, 24, 54, 71, 74]     38
[12, 24, 54, 71, 74]    38
[32, 56, 62, 65, 67]    38

When I run such code on Visual code it fails and the error is like this:

'TypeError: unhashable type: 'list'
1 Answers

You can convert values to tuples:

dftemp = df.groupby(df["Numbers"].apply(tuple)).Count_l.sum().sort_values(ascending = False )
Related