Stuck in a with pandas/sklearn

Im currenly stuck in a tensor flow/Neural network lesson. I cant find the error in the following code:

import pandas as pd
from pandas import np
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import Normalizer
from sklearn.compose import ColumnTransformer 
from sklearn.model_selection import train_test_split
dataset = pd.read_csv("life_expectancy.csv")
print(dataset.head())
print(dataset.describe())
dataset = dataset.drop(["Country"], axis = 1)
features = dataset.iloc[:, 0:-1]
labels = dataset.iloc[:, -1]
features = pd.get_dummies(features)
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size = 0.20, random_state = 12)
ct = ColumnTransformer([("only numeric", StandardScaler(), [features.select_dtypes(np.number)])], remainder='passthrough')
features_train_scaled = ct.fit_transform(features_train)

I allway get the following error, but dont have a clue what it means:

Traceback (most recent call last):
File “life_expectancy.py”, line 16, in
features_train_scaled = ct.fit_transform(features_train)
File “/usr/local/lib/python3.6/dist-packages/sklearn/compose/_column_transformer.py”, line 529, in fit_transform
self._validate_remainder(X)
File “/usr/local/lib/python3.6/dist-packages/sklearn/compose/_column_transformer.py”, line 320, in _validate_remainder
for cols in self._columns)
File “/usr/local/lib/python3.6/dist-packages/sklearn/compose/_column_transformer.py”, line 320, in
for cols in self._columns)
File “/usr/local/lib/python3.6/dist-packages/sklearn/utils/init.py”, line 258, in _determine_key_type
unique_key = set(key)
File “/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py”, line 1886, in hash
" hashed".format(self.class.name)
TypeError: ‘DataFrame’ objects are mutable, thus they cannot be hashed

Would be great if anyone could help

Did you happen to look up that type error?

Remember that dictionary keys are immutable types. You cannot have a dictionary key that is mutable.

See here:
https://techwithtech.com/series-objects-are-mutable-and-cannot-be-hashed/

2 Likes

Mutable objects are objects which value can be changed. For example, list is a mutable object, since you can append to it. int is an immutable object, because you can’t change it. The error saying one of your DataFrame column is a mutable object so it cannot be hashed. To use an object as a key in a dictionary, python needs to use its hash value, and that’s why you get an error.