Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Create a new column with random values based on conditional

Tags:

pandas

I've tried reading similar questions before asking, but I'm still stumped. Any help is appreaciated.

Input: I have a pandas dataframe with a column labeled 'radon' which has values in the range: [0.5, 13.65]

Output: I'd like to create a new column where all radon values that = 0.5 are changed to a random value between 0.1 and 0.5

I tried this:

df['radon_adj'] = np.where(df['radon']==0.5, random.uniform(0, 0.5), df.radon)

However, i get the same random number for all values of 0.5


I tried this as well. It creates random numbers, but the else statment does not copy the original values

df['radon_adj'] = df['radon'].apply(lambda x: random.uniform(0, 0.5) if x == 0.5 else df.radon)
like image 424
HolaGonzalo Avatar asked Mar 04 '26 18:03

HolaGonzalo


1 Answers

One way would be to create all the random numbers you might need before you select them using where:

>>> df = pd.DataFrame({"radon": [0.5, 0.6, 0.5, 2, 4, 13]})
>>> df["radon_adj"] = df["radon"].where(df["radon"] != 0.5, np.random.uniform(0.1, 0.5, len(df)))
>>> df
   radon  radon_adj
0    0.5   0.428039
1    0.6   0.600000
2    0.5   0.385021
3    2.0   2.000000
4    4.0   4.000000
5   13.0  13.000000

You could be a little smarter and only generate as many random numbers as you're actually going to need, but it probably took longer for me to type this sentence than you'd save. (It takes me 9 ms to generate ~1M numbers.)

Your apply approach would work too if you used x instead of df.radon:

>>> df['radon_adj'] = df['radon'].apply(lambda x: random.uniform(0.1, 0.5) if x == 0.5 else x)
>>> df
   radon  radon_adj
0    0.5   0.242991
1    0.6   0.600000
2    0.5   0.271968
3    2.0   2.000000
4    4.0   4.000000
5   13.0  13.000000
like image 101
DSM Avatar answered Mar 08 '26 22:03

DSM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!