Reputation: 9
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?
Upvotes: 0
Views: 32
Reputation: 14228
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
Upvotes: 0
Reputation: 9967
You can try...
df['difference']=np.where(df['1b']>df['1a'], df['1b']-df['1a'], 0)
Upvotes: 0
Reputation: 18296
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
to assign to a new column, you can do df["difference"] = ...
Upvotes: 2