mercredi 5 mai 2021

Replace NaN with random number

I have a dataframe of numbers such as:

A           B
2019-10-31  0.035333
2019-10-31  NaN
2019-11-30  -0.108532
2019-11-30  -0.030604
2019-11-30  NaN

I want to replace the NaN's in column B with a random gaussian number:

from random import seed
from random import gauss
# seed random number generator
seed(1)
# generate some Gaussian value
value = gauss(0, 0.1)

However, if I use the following code:

df.fillna(gauss(0, 0.1))

It fills all missing values with the same random value while I want a new random value for each NaN. How should I acchieve this?




Aucun commentaire:

Enregistrer un commentaire