Pandas的groupby方法报错:TypeError: unhashable type: 'numpy.ndarray'。

4

我有一个如图所示的数据框:

问题数据框:attdf

我想按源类和目标类对数据进行分组,计算每个组中的行数并总结注意力值。

在尝试实现时,我无法解决此类型错误:

---------------------------------------------------------------------------
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'])

我得到了一个<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f1e720f2080>,但我不确定如何使用它来获得我想要的结果。

Dataframe attdf可以从以下位置导入:https://drive.google.com/open?id=1t_h4b8FQd9soVgYeiXQasY-EbnhfOEYi

请给予建议。


3
请不要发布代码/数据的图片(或链接)。请提供创建数据框的代码,或将数据作为文本粘贴,以便用户可以复制它们以重现您的问题。 - anky
print(attdf.info()) 是什么? - jezrael
<class 'pandas.core.frame.DataFrame'> RangeIndex: 13264 entries, 0 to 13263 Data columns (total 5 columns): Source 13264 non-null int64 Destination 13264 non-null int64 Attention 13264 non-null float32 Source Class 13264 non-null object Destination Class 13264 non-null object dtypes: float32(1), int64(2), object(2) memory usage: 466.4+ KB 无 - achow
现在包括数据框的CSV链接。 - achow
2个回答

2
@Adam.Er8和@jezarael通过他们的输入帮助了我。在我的情况下,不可哈希类型错误是由于数据框中列的数据类型。原始数据框有两个对象列,我试图在groupby中使用它们,因此出现了不可哈希类型错误。但是,将数据直接从csv导入到新的数据框中可以解决数据类型问题。因此,不再遇到任何类型错误。原始df和从csv导入的df

0
尝试使用以下方式.agg
import pandas as pd

attdf = pd.read_csv("attdf.csv")

print(attdf.groupby(['Source Class', 'Destination Class']).agg({"Attention": ['sum', 'count']}))

输出:


                                 Attention      
                                       sum count
Source Class Destination Class                  
0            0                  282.368908  1419
             1                    7.251101    32
             2                    3.361009    23
             3                   22.482438   161
             4                   14.020189    88
             5                   10.138409    75
             6                   11.377947    80
1            0                    6.172269    32
             1                  181.582437  1035
             2                    9.440956    62
             3                   12.007303    67
             4                    3.025752    20
             5                    4.491725    28
             6                    0.279559     2
2            0                    3.349921    23
             1                    8.521828    62
             2                  391.116034  2072
             3                    9.937170    53
             4                    0.412747     2
             5                    4.441985    30
             6                    0.220316     2
3            0                   33.156251   161
             1                   11.944373    67
             2                    9.176584    53
             3                  722.685180  3168
             4                   29.776050   137
             5                    8.827215    54
             6                    2.434347    16
4            0                   17.431855    88
             1                    4.195519    20
             2                    0.457089     2
             3                   20.401789   137
             4                  378.802604  1746
             5                    3.616083    19
             6                    1.095061     6
5            0                   13.525333    75
             1                    4.289306    28
             2                    6.424412    30
             3                   10.911705    54
             4                    3.896328    19
             5                  250.309764  1132
             6                    8.643153    46
6            0                   15.249959    80
             1                    0.150240     2
             2                    0.413639     2
             3                    3.108417    16
             4                    0.850280     6
             5                    8.655959    46
             6                  151.571505   686

你对这个数据框还做了其他操作吗?我使用了你提供的 CSV 文件并且得到了正确的结果,没有任何错误。 - Adam.Er8
1
实际上不是这样的..我也成功导入了自己的CSV文件..我不明白问题出在哪里.. - achow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接