简体   繁体   English

如何为有条件的每一行生成一个随机数?

[英]How can I generate a random number for each row with condition?

I'm new at Python and I'll appreciate your help.我是 Python 的新手,非常感谢您的帮助。

I have a data frame with 2000 rows and 2 columns: Row and Pct.我有一个包含 2000 行和 2 列的数据框:行和百分比。 Basically, I want to create a third column that will be based on the following logic:基本上,我想创建基于以下逻辑的第三列:

  1. To generate a random number (between 0 to 1) for the first row - let's call this number X为第一行生成一个随机数(0 到 1 之间) - 我们称这个数为 X
  2. If X>Pct I want to add 1 to the new column and generate an additional random number for the first row and check again if X>Pct and if so - add 1 to the new column and generate an additional random number and so on.....如果 X>Pct 我想将 1 添加到新列并为第一行生成一个额外的随机数,然后再次检查 X>Pct 是否是这样 - 将 1 添加到新列并生成一个额外的随机数等等。 ……
  3. If X<=Pct I want to add 1 to the new column and move on to the next row, and so on.如果 X<=Pct 我想将 1 添加到新列并移动到下一行,依此类推。

Hope I managed to explain myself:)希望我设法解释自己:)

Thanks!谢谢!

Edit: For your questions:编辑:对于您的问题:

  1. It's just an example, I uploaded my df with a CSV file这只是一个例子,我用 CSV 文件上传了我的 df
  2. Adding 1 -> It means that the new column is basically empty (zero) and if the condition is true I want to add 1 to the proper row.添加 1 -> 这意味着新列基本上是空的(零),如果条件为真,我想将 1 添加到正确的行。 Basically it should act as a counter.基本上它应该作为一个计数器。
data = {
        'Pct': [0.8,0.4,0.3,0.7,0.3,1,0.23,0.75,0.93,0.6],
        'Row': [1,2,3,4,5,6,7,8,9,10]
}
df = pd.DataFrame(data, columns = ['Row','Pct'])
df

    Row Pct
0   1   0.80
1   2   0.40
2   3   0.30
3   4   0.70
4   5   0.30
5   6   1.00
6   7   0.23
7   8   0.75
8   9   0.93
9   10  0.60

You can do something like this:你可以这样做:

def generate_random_values(row):
    pct_value = float(row['Pct'])
    # 1 . Generate random no bw 0 and 1
    x = np.random.random()
    # 2. Init value of new column
    new_col = 0
    # 3. while x > pct_value, add 1 to new_col and generate new random no
    while x > pct_value:
        new_col += 1
        x = np.random.random()
    # 4. Here x < = pct_value, add 1 to new col and return for the current row
    new_col += 1
    return new_col

And then:接着:

df['new_column'] = df.apply(func=generate_random_values, axis=1)
print (df)
>>>
   Row   Pct  new_column
0    1  0.80           1
1    2  0.40           2
2    3  0.30           1
3    4  0.70           1
4    5  0.30           8
5    6  1.00           1
6    7  0.23           1
7    8  0.75           1
8    9  0.93           1
9   10  0.60           2

Also might be a good idea to check for a minimum threshold for the 'Pct' column before running the above function as you don't want to run into an infinite loop...在运行上述 function 之前检查“Pct”列的最小阈值也是一个好主意,因为您不想陷入无限循环......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python如何在Pandas数据帧中的每一行的范围内生成一个随机数? - Using Python how do I generate a random number within a range for each row in Pandas dataframe? 如何为 Python 中的列中的每组值生成一个随机数? - How can I generate a random number for each group of values in a column in Python? 如何对列表中的随机数样本进行编码,以便根据前面的数字生成的每个数字都遵循某些条件? - How can I code a sample of random numbers from a list, so that each number generated follows some condition depending on the preceding number? 如何在此代码中两次生成随机数? - How can I Generate the random number two times in this code? 如何在JavaScript和Python中生成相同的随机数? - How can I generate the same random number in JavaScript and Python? 我可以用 python 生成真实的随机数吗? - Can I generate authentic random number with python? Python 随机模块:如何生成包含特定数字的随机数? - Python random module: How can I generate a random number which includes certain digits? 我将如何生成一个新的随机数 - How would i generate a new random number Python中如何在循环中每次生成唯一的单个随机数? - How to generate a unique single random number each time in a loop in Python? 如何计算每行范围内的元素数 - How can I count the number of elements within a range in each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM