Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace a value in the entire pandas data frame

Tags:

python

pandas

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.

like image 512
data_person Avatar asked Mar 10 '23 13:03

data_person


1 Answers

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.

like image 54
Joe T. Boka Avatar answered Mar 21 '23 07:03

Joe T. Boka