关于 Pandas 的问题 -> Exception Ignored: Unhashable Type: 'numpy.ndarray'

时间:2021-02-23 16:05:14

标签: python pandas numpy exception typeerror

我通过按两个值分组创建了一个 Pandas DataFrame,并收到了一个类似于下面的表格:

<头>
名字 A 列
塞缪尔 [1,2,3]
约书亚 [4,5,6]
理查德 [7,8,9]
迈克尔 [7,8,9]
亨利 [7,8,9]

接下来,我按 A 列分组以计算每个数字列表有多少个唯一的名字,并收到以下错误:

<块引用>
Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
Traceback (most recent call last):
  File "pandas\_libs\hashtable_class_helper.pxi", line 4588, in > pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'numpy.ndarray'

当我返回一个索引为 numpy 数组类型的 pandas 系列时,我感到很困惑。为什么在这种情况下忽略了异常?

1 个答案:

答案 0 :(得分:0)

尝试通过 df['Column A'].map(tuple)df['Column A'].map(str) 将列表转换为元组

df.groupby(df['Column A'].map(tuple))['First Name'].unique()

Column A
(1, 2, 3)                     [Samuel]
(4, 5, 6)                     [Joshua]
(7, 8, 9)    [Richard, Michael, Henry]
Name: First Name, dtype: object