Skip to content Skip to sidebar Skip to footer

Python How To Map Categorical Values Into New Numeric Values Without Getting The Indices Must Be Integer Error?

I need to map some categorical values to integers. I tried the solution from this link at In[24]: title_mapping = {'Mr': 0, 'Miss': 1, 'Mrs': 2, 'Master': 3, 'Dr'

Solution 1:

The error is probably because you are trying to iterate from your train DataFrame instead of the list of DataFrames train_test_data.

Try simply doing:

train['Title'] = train['Title'].map(title_mapping)

The notebook you are basing on first creates a list of dataframes at [21]:

train_test_data = [train, test]

So when it is iterating at [24] it first goes thru train and then test fully, which it's whats needed when mapping a column.

By the way, if you ever want to iterate row by row from a DataFrame, do like this:

for index_value, rowin df.iterrows():
    print(index_value)
    # you can work foreachcolumnfrom that row:
    print(row['column_name'])

>>>0>>> I'm a column value

Post a Comment for "Python How To Map Categorical Values Into New Numeric Values Without Getting The Indices Must Be Integer Error?"