I try to translate a column of a Pandas Data Frame into int values using a mapping like this (asuming a given dataframe: my_dataframe and a colum: target_column):
targets = my_dataframe[target_column].unique()
map_to_int = {name: n for n, name in enumerate(targets)}
Using Python 3.6 with Pandas I wonder why
A)
my_dataframe['Integer-Column'] = map_to_int[my_dataframe[target_column]]
causes a
TypeError: 'Series' objects are mutable, thus they cannot be hashed
whileas
B)
my_dataframe['Integer-Column'] = my_dataframe[target_column].replace(map_to_int)
works fine.
I would like to understand why this happens. Is there any magic in replace that the TypeError is not thrown or am I missing something else? I already got the fact, that dict-keys are not allowed to be changeable. But still I have a hard time understanding this for real, since:
words = my_dataframe[target_column].unique()
# words = ['car' 'bike' 'plain']
foo = 'car'
map_to_int[foo] = 0
foo = 'bike'
map_to_int["bike"] = 1
Any attempt to help me understand why B) works without the trouble of A) would be appreciated.