简体   繁体   中英

How to create pandas dataframe variable/column based on two or more other variables?

I have a pandas dataframe, eg:

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

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

You can initialize the column with your final else value (eg 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM