renakre
renakre

Reputation: 8291

Finding the cells that contain the \N value and replacing them with 0

I have a big dataframe and when I process it I receive this error

ValueError: could not convert string to float: \N

To get rid of this error I need to replace the \N values with 0. I tried this code:

set1[set1 == '\N'] = 0 
#This also did not work --> set1[set1.ix[:,:] == '\N'] = 0

But, then I receive this error:

TypeError: Could not compare ['\N'] with block values

Any ideas?

Upvotes: 0

Views: 63

Answers (2)

a p
a p

Reputation: 3198

This is a good use for comprehensions.

set1 = [item if item != '\N' else 0 for item in set1]

(as with the other answer, this turns 'set1' into a Python list object; if it's something else and you want it to stay that way, this won't do the trick).

Upvotes: 1

Reaper
Reaper

Reputation: 757

set1 = map(lambda val: val if type(val) is str else 0, set1)

Assuming set1 is a list.

Upvotes: 1

Related Questions