Home > Mobile >  Python if condition over pandas df
Python if condition over pandas df

Time:10-16

I have the following df:

    Date        1a          1b
112 2022-10-17  3.18        2.11
298 2022-10-16  4.26        7.00
340 2022-10-16  11.66       7.80    
379 2022-10-16  15.78       2.13    

What I want (without a loop) to check for each row the value in 1b is larger than the value in 1a. And if so, I want to add a new column with the difference between the two. So, I want to obtain the following df:

    Date        1a          1b     difference
112 2022-10-17  3.18        2.11   0
298 2022-10-16  4.26        7.00   2.74
340 2022-10-16  11.66       7.80   0    
379 2022-10-16  15.78       2.13   0    

How can I do this?

CodePudding user response:

In [42]: df["1b"].sub(df["1a"]).clip(lower=0)
Out[42]:
112    0.00
298    2.74
340    0.00
379    0.00
dtype: float64
  • subtract 1a from 1b column
  • clip from the lower at 0
    • this means when 1a was greater, result would be negative, and so clamped at 0

to assign to a new column, you can do df["difference"] = ...

CodePudding user response:

You can try...

df['difference']=np.where(df['1b']>df['1a'], df['1b']-df['1a'], 0)

CodePudding user response:

You can use np.where, le and sub:

df['difference'] = np.where(df['1b'].le(df['1a']), 0, df['1b'].sub(df['1a']))

print(df):

           Date     1a    1b  difference
112  2022-10-17   3.18  2.11        0.00
298  2022-10-16   4.26  7.00        2.74
340  2022-10-16  11.66  7.80        0.00
379  2022-10-16  15.78  2.13        0.00
  • Related