如何在熊猫中标记具有多个条件的列?

时间:2019-02-07 08:24:31

标签: python pandas

我在熊猫中有以下数据框

 ID    Date         Weekday     Public_holiday      
 1     01-01-2018   Monday      0
 2     02-01-2018   Tuesday     0
 3     03-01-2018   Wednesday   1
 4     04-01-2018   Thursday    0
 5     05-01-2018   Friday      0
 6     06-01-2018   Saturday    1
 7     07-01-2018   Sunday      0

如果工作日为星期一,星期二,星期三,星期四,我想将其标记为0,但如果有公共假期,则应将其标记为31,如果工作日为Friday,但如果有公众假期,则应将其标记为32如果有周末,但如果周末有公众假期,则应仅将其标记为2

我正在熊猫后面追随

 m1 = df['Weekday'].isin(['Monday','Tuesday','Wednesday','Thursday'])
 m2 = df['Weekday'] == 'Friday'
 m3 = df['Public_Holiday'] == 1
 df['Flag'] = np.select([m1, m2, m3], ['0','1','2'],'3')

我想要的数据框是

ID    Date         Weekday     Public_holiday   Flag   
 1     01-01-2018   Monday      0               0
 2     02-01-2018   Tuesday     0               0  
 3     03-01-2018   Wednesday   1               3 
 4     04-01-2018   Thursday    0               0
 5     05-01-2018   Friday      0               1
 6     06-01-2018   Saturday    1               2
 7     07-01-2018   Sunday      0               2

1 个答案:

答案 0 :(得分:2)

这里有必要为不由Public_holiday链接的AND和与3链接的OR添加过滤条件,也未指定周末的默认值,因此添加到默认值:

m1 = df['Weekday'].isin(['Monday','Tuesday','Wednesday','Thursday'])
m2 = df['Weekday'] == 'Friday'
m3 = df['Public_holiday'] == 1

df['Flag'] = np.select([m1 & ~m3, m2 & ~m3, (m1 | m2) & m3], ['0','1','3'], default='2')

print (df)
   ID        Date    Weekday  Public_holiday Flag
0   1  01-01-2018     Monday               0    0
1   2  02-01-2018    Tuesday               0    0
2   3  03-01-2018  Wednesday               1    3
3   4  04-01-2018   Thursday               0    0
4   5  05-01-2018     Friday               0    1
5   6  06-01-2018   Saturday               1    2
6   7  07-01-2018     Sunday               0    2

您还可以在没有默认设置的情况下定义所有条件:

m1 = df['Weekday'].isin(['Monday','Tuesday','Wednesday','Thursday'])
m2 = df['Weekday'] == 'Friday'
m3 = df['Public_holiday'] == 1
m4 = df['Weekday'].isin(['Saturday','Sunday'])

df['Flag'] = np.select([m1 & ~m3, (m1 | m2) & m3, m4, m2 & ~m3], ['0','3','2','1'])