Peaceful
Peaceful

Reputation: 5480

Choose between two columns based on third column in pandas

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

Answers (1)

sophocles
sophocles

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

Related Questions