324
324

Reputation: 724

Python Dictionary Maps NaN's Instead of Contents

I have a pandas dataframe with several real estate listings. A subset of the dataset is as follows.

Neighborhood      High School      ...
WOODLEY           LIBERTY
WOODLEY 
COUNTRY CLUB  
COUNTRY CLUB      HERITAGE
COUNTRY CLUB      HERITAGE
COUNTRY CLUB      TUSCORORA
...

Many of the neighborhoods have no information and others are incorrect. I am trying to do a mapping to rectify this.

cleanHS = {"WOODLEY": "LIBERTY", "COUNTRY CLUB": "HERITAGE", ...}
dirty["High School"] = dirty["High School"].map(cleanHS)

Unfortunately, this results in the High School column having only NaN's. What am I doing wrong here?

Upvotes: 0

Views: 117

Answers (3)

325
325

Reputation: 606

dirty["High School"] = dirty["Neighborhood"].map(cleanHS)

If you are mapping high school to high school, you will not receive the desired result. High school district is derived from neighborhood, so you need to ensure the two columns are interacting.

Upvotes: 1

WhoAmI
WhoAmI

Reputation: 148

You need to change the column which you are trying to map

dirty["High School"] = dirty["Neighborhood"].map(cleanHS)

Upvotes: 1

Ric S
Ric S

Reputation: 9257

This is because you are mapping the values from High School to other values, but your starting column from which to map values should be Neighborhood

dirty["High School"] = dirty["Neighborhood"].map(cleanHS)

Upvotes: 2

Related Questions