pandas :根据条件创建具有随机值的新列 [英] Pandas: Create a new column with random values based on conditional

查看:103
本文介绍了 pandas :根据条件创建具有随机值的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试询问之前,我曾尝试阅读类似的问题,但我仍然感到困惑. 感谢您的帮助.

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

输入: 我有一个熊猫数据框,其中的列标记为" radon ",其值在以下范围内:[0.5,13.65]

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

输出: 我想创建一个新列,将所有= 0.5的ra值更改为0.1到0.5之间的随机值

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

我尝试过:

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

但是,对于所有0.5值,我都会得到相同的随机数

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

我也尝试过这个.它会创建随机数,但else陈述不会复制原始值

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)

推荐答案

一种方法是创建所有可能需要的随机数,然后再使用where选择它们:

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

您可能会更聪明一些,并且只会生成您实际需要的任意数量的随机数,但是我输入此句子的时间可能要比您节省的时间长. (我花了9毫秒才能生成约100万个数字.)

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.)

如果您使用x而不是df.radon,您的apply方法也将起作用:

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

这篇关于 pandas :根据条件创建具有随机值的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆