Python 类型错误:';系列';对象是可变的,因此不能用列对它们进行散列

Python 类型错误:';系列';对象是可变的,因此不能用列对它们进行散列,python,pandas,Python,Pandas,我的数据帧的一列有问题,但我不明白为什么我的列cat有问题 您的序列包含其他pd.series对象。这是个坏习惯。通常,您应该确保序列的类型是固定的,以便在不必显式检查类型的情况下执行操作 您的错误是由于pd.Series对象不可散列。一种解决方法是使用函数将pd.Series对象转换为可散列类型,例如tuple: s = pd.Series(['one string', 'another string', pd.Series([1, 2, 3])]) def converter(x):

我的数据帧的一列有问题,但我不明白为什么我的列cat有问题


您的序列包含其他
pd.series
对象。这是个坏习惯。通常,您应该确保序列的类型是固定的,以便在不必显式检查
类型的情况下执行操作

您的错误是由于
pd.Series
对象不可散列。一种解决方法是使用函数将
pd.Series
对象转换为可散列类型,例如
tuple

s = pd.Series(['one string', 'another string', pd.Series([1, 2, 3])])

def converter(x):
    if isinstance(x, pd.Series):
        return tuple(x.values)
    else:
        return x

res = s.apply(converter).unique()

print(res)

['one string' 'another string' (1, 2, 3)]

df_cat_tot['cat'].unique()
您正在尝试使用熊猫专用的
.cat
。您必须使用
['cat']
来访问该列。将来,请将数据帧作为文本而不是图像发布,输出/错误也是如此。感谢您的回答,我尝试使用df_cat_tot['cat'].unique()但我有相同的错误。您可以在df_cat_tot['cat']中发布
{type(i)for i的输出吗?值}
?请将代码粘贴为文本,这将有助于调试代码