vikky vikky - 10 days ago 8
Python Question

replace a value in the entire pandas data frame

df:

cat116_O cat116_S cat116_T cat116_U cat116_Y
0 0.0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0 0.0

expected output:

df(changed):

cat116_O cat116_S cat116_T cat116_U cat116_Y
0 -1 -1 -1 -1 -1
1 -1 -1 -1 -1 -1

code:

df.replace(0.0, -1)


But its not working. I was able to do iteratively for each row and column but it is taking a lot of time. Where am I going wrong with the replace function in the code.

Answer

Sounds like you're interested to find out why your replace function is not working.

I think this might be what you're looking for:

df.replace(to_replace = 0.0, value = -1, inplace = True)

This will return float(-1.0) values as your your values are floats.