重申一会儿循环,并从原始数据帧中删除循环提供的元素

时间:2019-04-30 11:28:14

标签: python python-3.x dataframe while-loop

我的代码似乎陷入了无限循环,很抱歉将所有代码都放在这里,但这是我可以帮助您的唯一方法。

代码:

def loader(data,truck):

    item_m = data["Weight"]
    item_v = data["Volume"]
    truck_m = truck["Weight"]
    truck_v = truck["Volume"]
    truck_c = truck["Price"]
    id_s = data["Pid"]

    item_mass,item_vol,truck_mass,truck_vol,truck_cost, id_series = tolist_converter(item_m,item_v,truck_m,truck_v,truck_c, id_s)
    lpstatus,total_cost,trucks_used, trucks_status, output_dict = allocator(item_mass,item_vol,truck_mass,
                                                                            truck_vol,truck_cost,id_series)

    items_dict = data[["Length","Width","Height","Pid" ]].set_index('Pid', drop =False).T.to_dict('list')
    truck["S.no"] = truck.index
    truck_dict = truck[["S.no","Length(ft)","Breadth(ft)","Height(ft)","TruckID"]].set_index('S.no').T.to_dict('list')


    string_d = string_preprocessing(output_dict, items_dict, truck_dict)
    left, loaded = packer(string_d)
    packed = packed_items(loaded)

    while len(left)>0:
        df1 = pd.DataFrame({'Pid': packed})
        df1['g'] = df1.groupby('Pid').cumcount()
        data['g'] = data.groupby('Pid').cumcount()
        dat = data[data.merge(df1, indicator=True, how='left')['_merge'].eq('left_only')]
        dat = dat.reset_index(drop=True)
        print(dat)

        item_m1 = dat["Weight"].tolist()
        item_v1 = dat["Volume"].tolist()

        # Mass & volume capacities of trucks
        truck_m = truck["Weight"].tolist()
        truck_v = truck["Volume"].tolist()

        # Cost of using each truck
        truck_c = truck["Price"].tolist()
        id_seri1 = dat["Pid"]

        items_dict1 = dat[["Length","Width","Height","Pid" ]].set_index('Pid', drop =False).T.to_dict('list')
        print("items_dc", items_dict)
        truck_dict1 = truck[["S.no","Length(ft)","Breadth(ft)","Height(ft)","TruckID"]].set_index('S.no').T.to_dict('list')


        # print(item_mass)
        lpstatus,value_obj,trucks_used, trucks_status, output_dict1 = allocator(item_m1,item_v1,truck_m,truck_v,truck_c,id_seri1)
        string_d1 = string_preprocessing(output_dict1, items_dict1, truck_dict1)
        lef, loade = packer(string_d1)
        packed1 = packed_items(loade)

        left = lef
        packed = packed1
        loaded.update((k + '_1' if k in loaded else k, v) for k, v in loade.items())
    return loaded

我不确定我是否以正确的方式申请while循环。这不是一个典型的堆栈问题,但是我已经花了相当长的一段时间了,并且无法理解问题所在。

我的目标:

def loader(数据,卡车):     运行代码以返回<已打包的列表     从原始数据框中删除已打包的项目     重新运行代码

0 个答案:

没有答案