0

Hi I am trying to make a new column based on combination of values in two columns

the original df I have is below

|  id | code_1   | code_2  |
| --- | -------- |  -----  |
| A0  | 001      |  X1     |
| A1  | 001      |  X2     |
| A2  | 001      |  x3     |
| A3  | 001      |  x4     |
| A4  | 002      |  X1     |
| A5  | 002      |  X2     |

regardless of the id in first column, I am only considering the combination of second and third column which are 'code_1' and 'code_2'

If the 'code_1' is 001 and 'code_2' is x1, x2 and x3, I want to create new column and give Y1

If the 'code_1' is 001 and 'code_2' is x4, then I want to give Y2

If the 'code_2' is 002 and 'code_2' is x1 and x2, then I want to give Z1

So, the final output for this table would look like below:

|  id | code_1   | code_2  |  new_code  |
| --- | -------- |  -----  |  --------  |
| A0  | 001      |  X1     |  Y1        |
| A1  | 001      |  X2     |  Y1        |
| A2  | 001      |  x3     |  Y1        |
| A3  | 001      |  x4     |  Y2        |
| A4  | 002      |  X1     |  Z1        |
| A5  | 002      |  X2     |  Z1        |

Thank you

mcsoini
  • 6,280
  • 2
  • 15
  • 38
  • 2
    Does [this](https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column) answer your question? – Mustafa Aydın May 28 '21 at 06:24

1 Answers1

0

Use numpy.select with chain conditions by & for bitwise AND and tested by Series.eq and Series.isin:

m1 = df['code_1'].eq('001') & df['code_2'].isin(['x1', 'x2', 'x3'])
m2 = df['code_1'].eq('001') & df['code_2'].eq('x4')
m2 = df['code_1'].eq('002') & df['code_2'].eq(['x1', 'x2'])

df['new_code'] = np.select([m1, m2, m3], ['Y1','Y2','Z1'])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252