Python group error, object "unhashable" Series
I have a Pandas DataFrame of values ββ(float) and country name strings ("UNITED STATES", "UNITED KINGDOM", etc.). I want to sum values ββbased on countries:
Data['Values'].groupby(Data['Country']).sum()
Unfortunately I am getting the following error:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Also, if I try to create a unique series composed of countries:
Data['Country'].unique()
I am getting the same error:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Why is this error occurring? Is this something about country names? Any help would be greatly appreciated. I am using Python 3.4 and Pandas 0.15.2.
source to share
Sometimes mutable types such as lists (or a series for that case) can sneak into your collection of immutable objects.
You can use apply
to make all objects immutable. Try
Data.Country = Data.Country.apply(str)
Data.groupby('Country').Values.sum()
Note that this may cause the lines not to be what you expected them to be; eg str(['Canada'])
β "['Canada']"
, so str(['Canada']) == 'Canada'
it gives False
. I recommend doing Data.Country.unique()
and at least visually checking to make sure everything looks the way it should.
source to share