从数据框中生成不同方式的场景

Generate szenarios with differnet means from data frame

我有以下数据框:

           Cluster  OPS(4)  mean(ln)  std(ln)
0           5-894  5-894a     2.203    0.775
1           5-894  5-894b     2.203    0.775
2           5-894  5-894c     2.203    0.775
3           5-894  5-894d     2.203    0.775
4           5-894  5-894e     2.203    0.775

对于每种手术类型(在 OPS(4) 列中),我想生成 10.000 个场景,这些场景应该存储在另一个数据框中。

我知道,我可以创建场景:

 num_reps = 10.000
 scenarios  = np.ceil(np.random.lognormal(mean, std, num_reps))

新数据框应该如下所示,每列有 10,000 个场景:

scen_per_surg = pd.DataFrame(index=range(num_reps), columns=merged_information['OPS(4)'])

OPS(4) 5-894a 5-894b 5-894c 5-894d 5-894e 
0         NaN    NaN    NaN    NaN    NaN    
1         NaN    NaN    NaN    NaN    NaN    
2         NaN    NaN    NaN    NaN    NaN    
3         NaN    NaN    NaN    NaN    NaN    
4         NaN    NaN    NaN    NaN    NaN    
5         NaN    NaN    NaN    NaN    NaN    
...

不幸的是,我不知道如何遍历第一个数据框的行来创建场景。

有人可以帮助我吗? 最好的问候

创建一些实验数据

import pandas as pd
df = pd.DataFrame(data=[
                          [ '5-894' , '5-894a'  ,   2.0 ,   0.70],
                          [ '5-894' , '5-894b'  ,   2.1 ,   0.71],
                          [ '5-894' , '5-894c'  ,   2.2 ,   0.72],
                          [ '5-894' , '5-894d'  ,   2.3 ,   0.73],
                          [ '5-894' , '5-894e'  ,   2.4 ,   0.74] ], columns =['Cluster', 'OPS(4)', 'mean(ln)', 'std(ln)'])
print(df)

创建一个空数据框

new_df = pd.DataFrame()

定义一个函数,将应用于原始df的每一行并生成所需的随机值并将其分配给新df中的列

import numpy as np
def geb_scenarios(row):
  # print(row)
  col, mean, std = row[1:]
  new_df[col] = np.ceil(np.random.lognormal(mean, std, 10))

应用函数

df.apply(geb_scenarios, axis=1)
print(new_df)