I have a pandas series, and I’d like to set all the values that are 0 to a random number between 100 and 1000. Is there a direct way of doing that? I’ve only seen a roundabout way like so:
nums = np.random.choice(range(100, 1100), size = 924)
df.loc[df['Target']==0] = nums
The error I get back here is "Must have equal len keys and value when setting with an iterable".
Size is 924 because I know that’s how many values I want to change.
>Solution :
you can use the .loc accessor with a boolean mask to select the rows where the value is 0, and then assign a random value between 100 and 1000 to those rows.
import pandas as pd
import numpy as np
# create a sample series
s = pd.Series([1, 2, 3, 0, 4, 0, 5, 6, 0])
# create a boolean mask to select the rows where the value is 0
mask = s == 0
# set the values to a random number between 100 and 1000
s.loc[mask] = np.random.randint(100, 1000, size=mask.sum())
print(s)
output –
0 1
1 2
2 3
3 784
4 4
5 208
6 5
7 6
8 923