pandas 类型错误:“系列”对象是可变的,因此它们不能被列散列问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50450672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'Series' objects are mutable, thus they cannot be hashed problemwith column
提问by Corentin Moreau
回答by jpp
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.
您的系列包含其他pd.Series
对象。这是不好的做法。通常,您应该确保您的系列是固定类型,以便您无需type
明确检查即可执行操作。
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
:
您的错误是由于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)]