2

I have pandas dataframe column which has integers and 'NA' values. Followinareis the unique values of the column

 print(testData.LotFrontage.unique())  

 ['80' '81' '74' '78' '43' '75' 'NA' '63' '85' '70' '26' '21' '24']

I need to replace this string NA with integer 0. I tried following code

NAReplace = {'NA': 0}

trainingData.LotFrontage = [NAReplace[item] for item in trainingData.LotFrontage ]

and I am getting an error

    trainingData.LotFrontage = [NAReplace[item] for item in trainingData.LotFrontage ]
KeyError: '65'

What is the reason for this issue? Is there any other way to do this?

2 Answers 2

1

Because, in the list comprehension, your getting the value i the dictionary for all value,

80 81 ..

The dictionary will be trying to find those keys that are also not 'NA', so you have to do get:

trainingData.LotFrontage = [NAReplace.get(item,item) for item in trainingData.LotFrontage ]

Also, pandasicer will be:

testData['LotFrontage'] = pd.to_numeric(testData['LotFrontage'],errors='coerce').fillna(0)

And another one from jpp's answer.

But to add something to jpp's first one:

testData['LotFrontage'].replace('NA', 0,inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

1

The reason is NAReplace is defined as a dictionary, and the syntax NAReplace[item] requires item is a key of NAReplace, otherwise you see a KeyError.

A list comprehension isn't appropriate here in any case. Just use fillna:

testData['LotFrontage'] = testData['LotFrontage'].replace('NA', 0)

Most likely you want numeric data, in which case I suggest you convert to numeric:

testData['LotFrontage'] = pd.to_numeric(testData['LotFrontage'], errors='coerce').fillna(0)

The argument errors='coerce' forces non-convertible values to give NaN.

1 Comment

Well, both at the same time on pd.to_numeric one.

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.