mardi 3 décembre 2019

Fill all occurrences of a value in a pandas dataframe with a different random number

I have a pandas dataframe that is like this:

Col1   Col2   Col3    
0      -1      0
-1      1      1
0       0      1

and I would like to replace all the occurrences of the value -1 with a random number, generated according to a uniform distribution. I have tried to use the replace function:

df.replace(-1,np.random.uniform(0,1))

but this way all the occurrences of value -1 are replaced with the same random number. Instead I would like them to be different, as in the following example:

Col1   Col2   Col3    
0      0.78    0
0.21    1      1
0       0      1

How can I do it?




Aucun commentaire:

Enregistrer un commentaire