Reputation: 167
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
Upvotes: 4
Views: 1045
Reputation: 109726
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'
Upvotes: 1
Reputation: 863531
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
Upvotes: 1