pandas Python groupby 错误,'unhashable' 系列对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27804729/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:48:56  来源:igfitidea点击:

Python groupby error, 'unhashable' Series object

pythonpython-3.xpandas

提问by crow_t_robot

I have a Pandas DataFrame of values (floats) and country name strings ("UNITED STATES", "UNITED KINGDOM", etc). I want to sum the values based on the countries:

我有一个包含值(浮点数)和国家/地区名称字符串(“美国”、“英国”等)的 Pandas DataFrame。我想总结基于国家的价值:

Data['Values'].groupby(Data['Country']).sum()

Unfortunately, I get the following error:

不幸的是,我收到以下错误:

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

Also, if I try to unique the Series consisting of the countries:

此外,如果我尝试独特的由国家组成的系列:

Data['Country'].unique()

I get the same error:

我犯了同样的错误:

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

Why is this error occurring? Is it something about the country names? Any help would be greatly appreciated. I am using Python 3.4 and Pandas 0.15.2.

为什么会发生此错误?与国名有关吗?任何帮助将不胜感激。我正在使用 Python 3.4 和 Pandas 0.15.2。

回答by ari

Sometimes mutable types like lists (or Series in this case) can sneak into your collection of immutable objects.

有时,像列表(或本例中的系列)这样的可变类型会潜入您的不可变对象集合中。

You can use applyto force all your objects to be immutable. Try

您可以使用apply强制所有对象不可变。尝试

Data.Country = Data.Country.apply(str)
Data.groupby('Country').Values.sum()

Note that this can result in strings not being what you expected them to be; e.g. str(['Canada'])-> "['Canada']"so str(['Canada']) == 'Canada'will yield False. I recommend doing Data.Country.unique()and at least visually inspecting to make sure that everything looks as it should.

请注意,这可能会导致字符串不是您期望的那样;例如str(['Canada'])->"['Canada']"所以str(['Canada']) == 'Canada'会产生False。我建议做Data.Country.unique()至少目视检查,以确保一切看起来都应该如此。