【问题标题】:Pandas function issues - equation output incorrectPandas 函数问题 - 方程输出不正确
【发布时间】:2020-04-21 15:19:38
【问题描述】:

row['conus_days']>0 或 row['conus_days1']>0: 返回行 ['conus_days']* 8 + 行['conus_days1']12 elif (row['Country']== '阿富汗' or row['Country']== 'Iraq' or row['Country']=='索马里' or row['Country']=='也门')和行['oconus_days']>0 或行['oconus_days1']>0: 返回行 ['oconus_days'] 12 + 行['oconus_days1']*8 elif (row['Country']== '阿富汗' or row['Country']== 'Iraq' or row['Country']=='索马里' or row['Country']=='也门') : 返回行 ['days_in_month']*12 elif (row['Country'] == 'Germany') 和 row['conus_days']>0: 返回行['conus_days']*8 + 行['conus_days1']10 elif (row['Country'] == '德国'): 返回行['days_in_month'] 10 elif row['Country'] == 'Conus': 返回行['working_days']* 8 别的: 返回行['working_days']*8 预测 ['小时']= predict.apply(lambda row: 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

【问题讨论】:

  • 您可以发布输入数据(作为文本)吗?这将使查找问题并为您提供解决方案变得更加容易。另外,输出中的哪个值应该是 288?
  • Moys- 输入是输出减去“小时”列”,最后一行应返回 288,国家 == 德国和 conus_days>0。

标签: pandas function lambda apply


【解决方案1】:

在双括号中关闭每个 if 语句允许每个 if 语句单独且准确地运行。

def get_hours(行): if ((row['Country']== '阿富汗' 或 row['Country']== '伊拉克' 或 row['Country']=='索马里' 或 row['Country']=='也门' ) 和 (row['conus_days']>0 或 row['conus_days1']>0)): 返回行 ['conus_days']* 8 + 行['conus_days1']12 if ((row['Country']== '阿富汗' 或 row['Country']== '伊拉克' 或 row['Country']=='索马里' 或 row['Country']=='也门' ) 和 row['oconus_days']>0 或 row['oconus_days1']>0): 返回行 ['oconus_days'] 12 + 行['oconus_days1']*8 if (row['Country']== '阿富汗' or row['Country']== '伊拉克' or row['Country']=='Somalia' or row['Country']=='Yemen') : 返回行 ['days_in_month']*12 if (((row['Country'] == 'Germany') 和 row['conus_days']>0): 返回行['conus_days']*8 + 行['conus_days1']*10 if ((row['Country'] == 'Germany') and row['oconus_days']>0): 返回行['oconus_days']*10 + 行['oconus_days1']8 if (row['Country'] == 'Germany'): 返回行['days_in_month'] 10 if (row['Country'] == 'Conus'): 返回行['working_days']* 8 别的: 返回行['working_days']*8 预测 ['小时']= predict.apply(lambda row: 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 康纳斯 2020 1
2 Mh 456789 2019-12-05 2020-03-12 康纳斯 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 ​​​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    相关资源
    最近更新 更多