1

I have a data frame I am trying to do predictions on house-prices based on the data such as square footage or if it has central air. I need to convert string values to numbers in order to model it. For example the values in the CentralAir column are 'N' or 'Y' which I want to be 0 and 1 respectively.

# pull data into target (y) and predictors (X) using other standard predictors; 
train_y2 = df_train.SalePrice

#convert strings to float so we can use predictors like the neighborhood and building type
central_air_mapping = {'N':0, 'Y':1}
df_train['CentralAir'] = df_train.map(central_air_mapping)

predictor_cols2 = ['CentralAir']

# Create training predictors data
train_X2 = df_train[predictor_cols2]

my_model2 = RandomForestRegressor()
my_model2.fit(train_X2, train_y2)

Then it returns

AttributeError: 'DataFrame' object has no attribute 'map'

2 Answers 2

1
def ToNum(c):
    if c == "Y":
        return 1
    else:
        return 0

df_train["CentralAir"] = df_train["CentralAir"].apply(ToNum)
Sign up to request clarification or add additional context in comments.

2 Comments

If I had 3 variables, like small(1), medium(2), and big(3) would I use ifelse? I never really coded too much
Simply, you can add another if else (elif) statement to that function, and return the desired value. Hope it helps
0

.map() is defined for Series, not DataFrames. This is why you are getting an error.

central_air_mapping = {'N':0, 'Y':1}
df_train['CentralAir'] = df_train['CentralAir'].map(central_air_mapping)
                                      ^^^^
                                 this was missing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.