Luca Nassano
Luca Nassano

Reputation: 21

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?

Upvotes: 1

Views: 202

Answers (1)

jezrael
jezrael

Reputation: 862691

First add parameter size to numpy.random.uniform and then replace values with DataFrame.mask:

print (np.random.uniform(0,1, size=df.shape))
[[0.45648576 0.82372149 0.04275742]
 [0.65523674 0.300342   0.94887199]
 [0.20756176 0.8776873  0.98973256]]

df = df.mask(df == -1, np.random.uniform(0,1, size=df.shape))
print (df)
       Col1      Col2  Col3
0  0.000000  0.519196     0
1  0.060452  1.000000     1
2  0.000000  0.000000     1

Upvotes: 1

Related Questions