Home > Blockchain >  Compare between each element in two Columns in a dataFrame in pandas to get the max element
Compare between each element in two Columns in a dataFrame in pandas to get the max element

Time:11-20

"I'm trying to compare between two columns in a dataFrame , i just want to start from the second element which in the first column "End" and compare it to the first element in second column "C.T.A", and the max value between them i will store it into a new column "Start" and so on. this is my attempt, but it throws an Error:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().*

And because i'm new to python i can't understand what is the problem :) ."

for i in range(0,8):
     for j in range(1,8):
        if sim_df.loc[i,'C.T.A'] >= sim_df.loc[i:8,'End']:
            sim_df.loc[j:8,'Start']=sim_df.loc[j:8,'C.T.A']
        elif sim_df.loc[i:8,'C.T.A'] <= sim_df.loc[i:8,'End']:
            sim_df.loc[j:8,'Start']=sim_df.loc[i:8,'End']

More Details: it's a simulation Table that the "End" Column Elements should be the sum of "Start" Elements "Serv. Duration" and then i should get the max between the two elements and store the max value in the "Start" column

   R.V  Time Interval  R.V For Services  C.T.A  Start  End  Serv. Duration
0    0              0                38      0      0    3               3
1  236              2                86      2      0    5               5
2  271              3                26      5      0    2               2
3  390              5                80     10      0    4               4
4  134              2                 0     12      0    2               2
5  961             14                55     26      0    3               3
6  441              5                79     31      0    4               4
7  158              2                35     33      0    3               3
__________________________________________________________________________

CodePudding user response:

Не очень понял, но вроде бы должно быть так

df['Start'] = [max(x,y) for x,y in zip(df['C.T.A'], df['End'])]

CodePudding user response:

i tried that : df['Start'] = [max(x,y) for x,y in zip(df['C.T.A'], df['End'])] the output :

   C.T.A    Start     End    Serv. Duration
0        0      0       4                4
1        8      8       10               2
2       10     10       13               3
3       24     24       27               3
4       29     29       33               4
5       30     30       32               2
6       33     33       38               5
7       40     40       45               5

But it should be :

  C.T.A    Start     End    Serv. Duration
0        0      0       4                4
1        8      8       10               2
2       10     10       13               3
3       24     24       27               3
4       29     29       33               4
5       30     33       37               2
6       33     37       42               5
7       40     42       47               5
  • Related