我在熊猫中有以下数据框
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
,如果工作日为Friday
,但如果有公众假期,则应将其标记为3
,
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
答案 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'])