1

I am trying to do a Pandas IF then Else statement in a new column. I couldn't find any questions where people used write in their own strings or "Yes"/"No".

Trying to do the following logic, If col A = Col B, then No Else Yes.

enterprisehr['Change_Dept'] = np.select([enterprisehr['Dept_Descr'] == enterprisehr['Old_Dept_Descr']], "No", "Yes")

I am getting this error: ValueError: list of cases must be same length as list of conditions

Should I be using numpy here to do this? How can I write my own values in, ie "No"/"Yes".

Sample Data /Expectation:

enter image description here

2
  • why don't you provide your data as text and expected output? Commented Sep 16, 2021 at 14:26
  • ok will do , give me a few. Commented Sep 16, 2021 at 14:27

2 Answers 2

2

Numpy's select is expecting the choice list to be the same length as the condition list. Use where to choose between two values.

import pandas as pd
import numpy as np

enterprisehr = pd.DataFrame({'Dept_Descr': ['A', 'B', 'C'],
                             'Old_Dept_Descr': ['A', 'B', 'X']})

condition = enterprisehr['Dept_Descr'] == enterprisehr['Old_Dept_Descr']

enterprisehr['Change_Dept'] = np.where(condition, "No", "Yes")
print(enterprisehr)

Output:

  Dept_Descr Old_Dept_Descr Change_Dept
0          A              A          No
1          B              B          No
2          C              X         Yes

Note that I also removed the square brackets from around the condition statement so that condition is a Pandas Series object that isn't wrapped in list.

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

Comments

1

for np.select

x = np.arange(10)

condlist = [x<3, x>5]

choicelist = [x, x**2]

np.select(condlist, choicelist)

length of condlist and choicelist should be same

you can also use np.where syntax:

numpy.where(condition[, x, y])

example:

x = np.where(x>40, 4, 0) + np.where(x>50, 1, 0)

2 Comments

Thanks Raj, can this be done on strings too? I am not using numbers in my columns.
yes it can be used for any data types.

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.