Reputation: 10051
I have such dataframe, how can I fillna
with random float values from -0.5
to 0.5
(bounds included) for value
column?
city district date value
0 a b 2019/8/1 0.15
1 a b 2019/9/1 0.12
2 a b 2019/10/1 NaN
3 c d 2019/8/1 0.03
4 c d 2019/9/1 -0.36
5 c d 2019/10/1 NaN
Thank you.
Upvotes: 1
Views: 1585
Reputation: 66
You can use apply method:
def func(val):
if np.isnan(val):
return random.uniform(-0.5, 0.5)
else:
return val
df['value'] = df['value'].apply(func)
Upvotes: 1
Reputation: 863226
If want replace each NaN
s use Series.mask
:
df['value'] = df['value'].mask(df['value'].isna(), np.random.uniform(-0.5, 0.5, size=len(df)))
Or numpy.where
:
df['value'] = np.where(df['value'].isna(),
np.random.uniform(-0.5, 0.5, size=len(df)),
df['value'])
print (df)
city district date value
0 a b 2019/8/1 0.150000
1 a b 2019/9/1 0.120000
2 a b 2019/10/1 0.227054
3 c d 2019/8/1 0.030000
4 c d 2019/9/1 -0.360000
5 c d 2019/10/1 -0.001102
Upvotes: 3
Reputation: 10531
You need to specify what you mean by "random". Assuming you mean "uniformly at random", you can use fill_na
:
df.value = df.value.fillna(
pd.Series(np.random.uniform(-0.5, 0.5, size=len(df)), index=df.index)
)
Upvotes: 2