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

前端之家收集整理的这篇文章主要介绍了python-如何标记熊猫中具有多个条件的列? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在熊猫中有以下数据框

 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,但是如果有公共假日,则应将其标记为3,
如果工作日是星期五,则为1,但如果有公共假期,
2,如果有周末,但是如果周末有公共假期,则应将其标记为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
最佳答案
对于非由AND链接的Public_holiday过滤器和与OR链接的3过滤器,以下条件是必要的添加条件,也未指定周末的默认值,因此添加到默认值:

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

df['Flag'] = np.select([m1 & ~m3,m2 & ~m3,(m1 | m2) & m3],'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','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],'3','2','1'])

猜你在找的Python相关文章