0

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?

3 Answers 3

2
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"] = ...

Sign up to request clarification or add additional context in comments.

Comments

0

You can try...

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.