Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据另一列的条件应用_Python_Pandas - Fatal编程技术网

Python 根据另一列的条件应用

Python 根据另一列的条件应用,python,pandas,Python,Pandas,我希望根据另一列中的条件调整一列的值 我使用的是np.busday\u count,但我不希望周末值表现得像周一(周六到周二有1个工作日,我希望是2个) 样本: dayofweek dispatch_working_days 43159 1.0 3 48144 3.0 3 45251 6.0 1 49193 3.0 0 42470 3.0 1 47874 6.0 1 44500 3.0

我希望根据另一列中的条件调整一列的值

我使用的是np.busday\u count,但我不希望周末值表现得像周一(周六到周二有1个工作日,我希望是2个)

样本:

            dayofweek   dispatch_working_days
    43159   1.0 3
    48144   3.0 3
    45251   6.0 1
    49193   3.0 0
    42470   3.0 1
    47874   6.0 1
    44500   3.0 1
    43031   6.0 3
    43193   0.0 4
    43591   6.0 3
预期成果:

        dayofweek   dispatch_working_days
43159   1.0 3
48144   3.0 3
45251   6.0 2
49193   3.0 0
42470   3.0 1
47874   6.0 2
44500   3.0 1
43031   6.0 2
43193   0.0 4
43591   6.0 4
目前,我正在使用这个for循环将一个工作日添加到周六和周日值中。太慢了


我可以用矢量化来加快速度吗。我尝试了使用.apply,但没有效果。

我很确定这是可行的,但还有更优化的实现:

def adjust_dispatch(df_line):
    if df_line['dayofweek'] >= 5:
        return df_line['dispatch_working_days'] + 1
    else:
        return df_line['dispatch_working_days']         

df['dispatch_working_days'] = df.apply(adjust_dispatch, axis=1)

中的
可以用该行替换代码:

或者您可以使用
numpy.where


你能发布你想看到的结果吗?是的,在中添加了它。基本上,任何一行等于5或6的dayofweek都需要将dispatch_working_days的值增加+1。我认为这行代码可以给你一些加速:
dispdf=df.dropna(子集=['dispatched_at','Selled_at'])
dispdf[“dispatch_working_days”]=np.busday_计数(dispdf.Selled_at.values.ast.astType('datetime64[D]),dispdf.dispatched_at.values.astype('datetime64[D]'))
def adjust_dispatch(df_line):
    if df_line['dayofweek'] >= 5:
        return df_line['dispatch_working_days'] + 1
    else:
        return df_line['dispatch_working_days']         

df['dispatch_working_days'] = df.apply(adjust_dispatch, axis=1)
dispdf.loc[dispdf.dayofweek>5,'dispatch_working_days']+=1