Home > Software engineering >  multiply 2 columns until get a desired value
multiply 2 columns until get a desired value

Time:11-30

Greeting everyone,

I have this table (Without the Res_Problem):

ID Problem X Impact Prob Res_Problem
ID1 12 IDC1 1 2 (12-2)=10
ID1 12 IDC2 2 2 (10-4)=6 STOP
ID1 12 IDC3 1 0 NO LOOP
ID1 12 IDC4 1 0 NO LOOP
ID2 10 IDB1 1 2 New Loop (10-2)=8
ID2 10 IDB1 1 2 (8-2) = 6 STOP

I want to do a loop that multiplies the Impact and prob until get a desire value (6 for example),and stop the loop until it reach the 6. but start again the loop on the ID2... and so on, any suggestions?

I think it has to be something like this :

while (df['Problem'] - df['Impact']*df['Impact'] < 6): df['loop'] = res

The loop should create the 'Res_Problem' column

CodePudding user response:

Here is one option:

s = (df['Problem']
 .sub(df['Impact'].mul(df['Prob'])
      .groupby(df['ID']).cumsum()
      )
)

m = s.le(6).groupby(df['ID']).shift(fill_value=False)

df['Res_Problem'] = s.mask(m)

output:

    ID  Problem     X  Impact  Prob  Res_Problem
0  ID1       12  IDC1       1     2         10.0
1  ID1       12  IDC2       2     2          6.0
2  ID1       12  IDC3       1     0          NaN
3  ID1       12  IDC4       1     0          NaN
4  ID2       10  IDB1       1     2          8.0
5  ID2       10  IDB1       1     2          6.0
  • Related