行['conus_days']> 0或行['conus_days1']> 0: 返回行['conus_days'] * 8 +行['conus_days1'] 12 elif(row ['Country'] =='Afghanistan'或row ['Country'] =='Iraq'或row ['Country'] =='Somalia'或row ['Country'] =='也门')并且row ['oconus_days']> 0或row ['oconus_days1']> 0: 返回行['oconus_days'] 12 +行['oconus_days1'] * 8 elif(row ['Country'] =='Afghanistan'或row ['Country'] =='Iraq'或row ['Country'] =='Somalia'或row ['Country'] =='也门') : 返回行['days_in_month'] * 12 elif(row ['Country'] =='Germany')和row ['conus_days']> 0: 返回row ['conus_days'] * 8 + row ['conus_days1'] 10 elif(row ['Country'] =='Germany'): 返回行['days_in_month'] 10 elif row ['Country'] =='Conus': 返回行['working_days'] * 8 其他: 返回行['working_days'] * 8 预测['hours'] = Forecast.apply(lambda行:get_hours(row),axis = 1) 打印(forecast.head())
这将返回以下输出:
Name EID Start Date End Date Country year Month \
0 xx 123456 2019-08-01 2020-01-03 Afghanistan 2020 1
1 XX 3456789 2019-09-22 2020-02-16 Conus 2020 1
2 xx. 456789 2019-12-05 2020-03-12 Conus 2020 1
3 DR. 789456 2019-09-11 2020-03-04 Iraq 2020 1
4 JR. 985756 2020-01-03 2020-05-06 Germany 2020 1
days_in_month start_month end_month working_days conus_mth oconus_mth \
0 31 2020-01-01 2020-01-31 21 8 1
1 31 2020-01-01 2020-01-31 21 9 2
2 31 2020-01-01 2020-01-31 21 12 3
3 31 2020-01-01 2020-01-31 21 9 3
4 31 2020-01-01 2020-01-31 21 1 5
conus_days conus_days1 oconus_days oconus_days1 hours
0 0 0 2 25 224
1 0 0 0 0 168
2 0 0 0 0 168
3 0 0 0 0 372
4 1 28 0 0 344
-第4行的输出不正确,应返回288
答案 0 :(得分:0)
通过用双括号关闭每个if语句,可以使每个if语句单独且准确地运行。
def get_hours(row): 如果((row ['Country'] =='Afghanistan'或row ['Country'] =='Iraq'或row ['Country'] =='Somalia'或row ['Country'] =='也门)和(row ['conus_days']> 0或row ['conus_days1']> 0)): 返回行['conus_days'] * 8 +行['conus_days1'] 12 如果((row ['Country'] =='Afghanistan'或row ['Country'] =='Iraq'或row ['Country'] =='Somalia'或row ['Country'] =='也门)和row ['oconus_days']> 0或row ['oconus_days1']> 0): 返回行['oconus_days'] 12 +行['oconus_days1'] * 8 if(row ['Country'] =='Afghanistan'或row ['Country'] =='Iraq'或row ['Country'] =='Somalia'或row ['Country'] =='Yemen') : 返回行['days_in_month'] * 12 if((row ['Country'] =='Germany')and row ['conus_days']> 0): 返回row ['conus_days'] * 8 + row ['conus_days1'] * 10 if((row ['Country'] =='Germany')and row ['oconus_days']> 0): 返回row ['oconus_days'] * 10 + row ['oconus_days1'] 8 if(row ['Country'] =='Germany'): 返回行['days_in_month'] 10 如果(row ['Country'] =='Conus'): 返回行['working_days'] * 8 其他: 返回行['working_days'] * 8 预测['hours'] = Forecast.apply(lambda行:get_hours(row),axis = 1) 打印(forecast.head())
Name EID Start Date End Date Country year Month \
0 XX。 123456 2019-08-01 2020-01-03阿富汗2020 1
1 xx 3456789 2019-09-22 2020-02-16 Conus 2020 1
2 Mh 456789 2019-12-05 2020-03-12 Conus 2020 1
3 DR 789456 2019-09-11 2020-03-04伊拉克2020 1
4 JR 985756 2020-01-03 2020-05-06德国2020年1
days_in_month start_month end_month Working_days conus_mth oconus_mth \
0 31 2020-01-01 2020-01-31 21 8 1
1 31 2020-01-01 2020-01-31 21 9 2
2 31 2020-01-01 2020-01-31 21 12 3
3 31 2020-01-01 2020-01-31 21 9 3
4 31 2020-01-01 2020-01-31 21 1 5
conus_days conus_days1 oconus_days oconus_days1小时
0 0 0 2 25 224
1 0 0 0 0 168
2 0 0 0 0 168
3 0 0 0 0 372
4 1 28 0 0 288