George Zambrano
George Zambrano

Reputation: 47

How do I replace a value for a string within multiple columns in pandas

I used .replace() to replace the value -99 to 0 on my dataset. However, it only replaced the value -99 for the columns that were all strings meaning that the columns that contained strings still have -99 within them. I would like to replace -99 with the string 'unknown' for the columns that contain strings.

enter image description here

Upvotes: 0

Views: 203

Answers (1)

Clem G.
Clem G.

Reputation: 396

You should just use the replace method 2 times, one like you did to replace numeric values, the second to replace string values like this :

df.replace(-99, 0, inplace=True)
df.replace('-99', 'unknown', inplace=True)

Upvotes: 1

Related Questions