Search code examples
pythonpandasdictionarydatasetindices

Python try to use .map()


I am learning Machine Learning and trying to write a code from myself using the Iris Dataset.

I open the dataset with pandas and then I am trying to pass a dictionary in my dataset to convert the last column from Strings into Int but when try this:

dataset.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'class']

class_mapping = {'Iris-setosa': 1, 'Iris-versicolor': 2, 'Iris-virginica': 3}
for classe in dataset :
    classe['class'] = classe['class'].map(class_mapping)

PyCharm returns me this: TypeError: string indices must be integers


Solution

  • Finally I managed to solve this problem. Instead of using for loop, I used this:

        dataset ['class'] = dataset ['class']. map (class_mapping)

    I didn't need a for loop because .map iterates for me.