简体   繁体   中英

TypeError: 'Series' objects are mutable, thus they cannot be hashed problemwith column

I have a problem with a column of my dataframe but I don't understand why there are trouble on my column cat. 在此处输入图像描述

在此处输入图像描述

Your series contains other pd.Series objects. This is bad practice. In general, you should ensure your series is of a fixed type to enable you to perform manipulations without having to check for type explicitly.

Your error is due to pd.Series objects not being hashable. One workaround is to use a function to convert pd.Series objects to a hashable type such as 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()

This will help you to recover from this error. Both syntaxes are correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM