生成每次迭代更新的动态滞后变量的最快方法是什么?

时间:2017-09-18 13:49:48

标签: python pandas

我有几十万个组,通过它我想迭代这个特定的滞后操作。以下是一个示例,其中Buy_Ord_No是变量分组:

Sample Table\e

我想生成Lag_Exec_Qty和Exec_Qty。我在这里做的最初是在Buy_Act_Type = 1或Buy_Act_Type = 4时将Exec_Qty设置为等于0.然后,我采用Exec_Qty的滞后值和Lag_Exec_Qty。在同一行中,我总结了Trd_Qty和Lag_Exec_Qty来获取更新的Exec_Qty。

这是我目前的代码:

for b in buy:
    temp=buy_sorted_file[buy_sorted_file["Buy_Ord_No"]==b]
    temp=temp.sort_values(["Buy_Ord_No","Buy_Ord_Txn_Time"], ascending=[True, True]).reset_index(drop=True)

    for index in range(len(temp.index)):
        if(int(temp["Buy_Act_Type"].iloc[index])==1 or int(temp["Buy_Act_Type"].iloc[index])==4):
            temp["Exec_Qty"].iloc[index]=0
            temp["Lag_Exec_Qty"].iloc[index]=0
        else:
            temp["Lag_Exec_Qty"].iloc[index]=temp["Exec_Qty"].iloc[index-1]
            temp["Exec_Qty"].iloc[index]=temp["Trd_Qty"].iloc[index]+temp["Lag_Exec_Qty"].iloc[index]
    if (len(buy_sorted_exec_file.index) == 0):
        buy_sorted_exec_file = temp.copy()
    else:
        buy_sorted_exec_file = pd.concat([temp,buy_sorted_exec_file]).reset_index(drop=True)
buy_sorted_file= buy_sorted_exec_file.sort_values(["Buy_Ord_Txn_Time", "Buy_Ord_Limit_Pr"],ascending=[True, True]).reset_index(drop=True)

代码需要很长时间才能运行。无论如何,我可以加快这个过程吗?

1 个答案:

答案 0 :(得分:0)

你应该能够做到,没有任何循环:

?geom_bar