逐行应用自定义函数,并带有break

时间:2019-04-07 21:03:52

标签: python pandas

我有一个数据框,我想逐行迭代一个自定义函数,但是直到达到特定目标为止,并且,当条件满足时,我想将结果附加到新列中,然后循环转到下一行。

actual scenario, note that (i) is the same for all lines and i want to do that calculation for each row

下面是我的代码(我是python的新手,因此,我们将不胜感激)

def converter(a): 
    a=df_agg_70
    for i in df_agg_70:        
        for i in range(0,10000):            
            global qtd, vlr_stop, sku,var            
            sku = a['sku']
            total = a['Qtd Vendida']
            vlr_at = a['Vlr Vendido']
            conv = (((total/i)*(a['Preco_Sell_In']).astype(int))*100)/vlr_at
            vlr_stop = ((total/i) *  (a['Preco_Sell']).astype(int))
            var = conv
            var_2 = round(var[0],0)
            qtd = (total/i)
            a['var_3'] = var[0]
            a['qtd'] = qtd[0]
            a['vlr_stop'] = vlr_stop[0]
我想在变量(var)获得目标时打破代码,然后将计算重新启动到另一行
        if var[0] > 50 and var[0] < 100:
            break
        return  pd.DataFrame({"sku":sku,"qtd":qtd,"valor":vlr_stop.astype(int),"var":var})

当前结果仅考虑第一行并适用于所有数据集

0 个答案:

没有答案