Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set values in pandas series to a random value based on condition

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading