Reputation: 5480
I have a Pandas dataframe containing three columns say A, B, C. I want to add column D to the dataframe whose values are taken from A and B based on values in C. For example, if C < 0.5, then D should contain value from B, otherwise value from A. How can I achieve this?
Upvotes: 1
Views: 541
Reputation: 13831
You can use np.where
for this:
import numpy as np
df['D'] = np.where(df['C'].lt(0.5),df['B'],df['A'])
This reads as, for each row, if value in C is less than 0.5, return value from column B, otherwise, return value from column A.
Note that you can replace lt(0.5)
with le(0.5)
to say less than or equal to 0.5.
Upvotes: 4