Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby throws: TypeError: unhashable type: 'numpy.ndarray'

I have a dataframe as shown in the picture:

problem dataframe: attdf

I would like to group the data by Source class and Destination class, count the number of rows in each group and sum up Attention values.

While trying to achieve that, I am unable to get past this type error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-100-6f2c8b3de8f2> in <module>()
----> 1 attdf.groupby(['Source Class', 'Destination Class']).count()

8 frames
pandas/_libs/properties.pyx in pandas._libs.properties.CachedProperty.__get__()

/usr/local/lib/python3.6/dist-packages/pandas/core/algorithms.py in _factorize_array(values, na_sentinel, size_hint, na_value)
    458     table = hash_klass(size_hint or len(values))
    459     uniques, labels = table.factorize(values, na_sentinel=na_sentinel,
--> 460                                       na_value=na_value)
    461 
    462     labels = ensure_platform_int(labels)

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.factorize()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable._unique()

TypeError: unhashable type: 'numpy.ndarray'
attdf.groupby(['Source Class', 'Destination Class'])

gives me a <pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f1e720f2080> which I'm not sure how to use to get what I want.

Dataframe attdf can be imported from : https://drive.google.com/open?id=1t_h4b8FQd9soVgYeiXQasY-EbnhfOEYi

Please advise.

like image 700
a_jelly_fish Avatar asked Oct 31 '25 03:10

a_jelly_fish


1 Answers

@Adam.Er8 and @jezarael helped me with their inputs. The unhashable type error in my case was because of the datatypes of the columns in my dataframe.

Original df and df imported from csv

It turned out that the original dataframe had two object columns which i was trying to use up in the groupby. Hence the unhashable type error. But on importing the data into a new dataframe right out of a csv fixed the datatypes. Consequently, no type errors faced anymore.

like image 100
a_jelly_fish Avatar answered Nov 02 '25 18:11

a_jelly_fish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!