Home > Mobile >  Numpy: if then else on new column , using new strings for THEN and ELSE
Numpy: if then else on new column , using new strings for THEN and ELSE

Time:09-17

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

CodePudding user response:

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.

CodePudding user response:

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)
  • Related