4

I have a pandas dataframe, e.g.:

Col1 Col2
A     1 
B     2
C     3

I understand how to create a Col3 based on say the value of Col2:

df['Col3'] = (df['Col2'] <= 1).astype(int)

But ... How about if the new column was based on two variables, as in (pseudocode here):

if Col2=1 and Col3=1 then Col4='X'
else if Col2=1 and Col3=2 then Col4='Y'
else Col4='Z'

how would that be achieved? many thanks

0

2 Answers 2

1

You can try double numpy.where:

df['Col4'] = np.where((df['Col2'] == 1) & (df['Col3'] == 1), 'X', 
             np.where((df['Col2'] == 1) & (df['Col3'] == 2), 'Y', 'Z'))

Sample:

import pandas as pd

df = pd.DataFrame({'Col2': {0: 1, 1: 1, 2: 3}, 
                   'Col1': {0: 'A', 1: 'B', 2: 'C'}, 
                   'Col3': {0: 1, 1: 2, 2: 4}})
print (df)

  Col1  Col2  Col3
0    A     1     1
1    B     1     2
2    C     3     4

df['Col4'] = np.where( (df['Col2'] == 1) & (df['Col3'] == 1), 'X', 
             np.where((df['Col2'] == 1) & (df['Col3'] == 2), 'Y', 'Z'))

print (df)
  Col1  Col2  Col3 Col4
0    A     1     1    X
1    B     1     2    Y
2    C     3     4    Z

Another solution with loc and fillna for fill NaN all other values:

df.loc[ (df['Col2'] == 1) & (df['Col3'] == 1) , 'Col4'] =  'X'
df.loc[ (df['Col2'] == 1) & (df['Col3'] == 2) , 'Col4'] =  'Y'
df['Col4'] = df['Col4'].fillna('Z')

print (df)
  Col1  Col2  Col3 Col4
0    A     1     1    X
1    B     1     2    Y
2    C     3     4    Z
Sign up to request clarification or add additional context in comments.

Comments

1

You can initialize the column with your final else value (e.g. Z) and then check each condition:

df['Col4'] = 'Z'
df.loc[(df.Col1 == 1) & (df.Col3 == 1), 'Col4'] = 'X'
df.loc[(df.Col2 == 1) & (df.Col3 == 2), 'Col4'] = 'Y'

1 Comment

Thanks. this and the previous answers work really well.

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.