跳霹雳舞的蒙特卡 2020-12-25 20:13 采纳率: 37.5%
浏览 9

pandas 提速方法是什么?

请问我写的Pandas,如何进行优化和提速?


def pick_SR_1(buyer_information):
    global output
    global seller_SR
    check_information = [x for x in buyer_information[4:14] if x == x]
    for i in range(0, len(check_information), 2):
        if check_information[i] != '品牌' and check_information[i] != '等级':
            check_information[i + 1] = int(check_information[i + 1])
    seller_pick_goods = seller_SR[seller_SR['货物数量(张)'] != 0]

    for i in range(0, len(check_information), 2):
        if seller_pick_goods[(seller_pick_goods[check_information[i]] == check_information[i + 1])]['货物数量(张)'].sum() >= \
                buyer_information[2]:
            seller_pick_goods = seller_pick_goods[(seller_pick_goods[check_information[i]] == check_information[i + 1])]
    start_pick_seller = seller_pick_goods.sort_values(by=['仓库', '货物数量(张)'], ascending=False)
    start_pick_seller['累计货物数量(张)'] = start_pick_seller['货物数量(张)'].cumsum()
    start_pick_seller['是否拣选'] = 1
    start_pick_seller['是否拣选'].where(start_pick_seller['累计货物数量(张)'] <= buyer_information[2], 0, inplace=True)
    final_pick_goods_pd = start_pick_seller[start_pick_seller['是否拣选'] == 1]
    if len(final_pick_goods_pd) != 0 and final_pick_goods_pd['累计货物数量(张)'].values[-1] < buyer_information[2]:
        final_pick_goods_pd = final_pick_goods_pd.append(start_pick_seller.iloc[len(final_pick_goods_pd), :])
    elif len(final_pick_goods_pd) == 0:
        final_pick_goods_pd = final_pick_goods_pd.append(start_pick_seller.iloc[0, :])
    final_pick_goods_index = final_pick_goods_pd.index
    final_pick_goods = final_pick_goods_pd['货物数量(张)'].values
    if len(final_pick_goods) == 1:
        final_pick_goods[0] = buyer_information[2]
    else:
        final_pick_goods[-1] = buyer_information[2] - final_pick_goods_pd['累计货物数量(张)'].values[-2]

    judge_binary_list = seller.index.isin(final_pick_goods_index)
    last_pick_goods_number = seller.loc[final_pick_goods_index[-1], '货物数量(张)']
    seller.loc[judge_binary_list, '货物数量(张)'] = seller.loc[judge_binary_list, '货物数量(张)'].apply(cut_test)
    seller.loc[final_pick_goods_index[-1], '货物数量(张)'] = last_pick_goods_number - final_pick_goods[-1]
    final_pick_goods_satisfy = final_pick_goods_pd.apply(check_satisfy, args=(check_information,), axis=1).tolist()

    len_of_final_index = len(final_pick_goods_index)
    output_append = pd.DataFrame(
        {'买方客户': [buyer_information[0]] * len_of_final_index, '卖方客户': seller.iloc[final_pick_goods_index, 0].tolist(),
         '品种': ['SR'] * len_of_final_index, '货物编号': seller.iloc[final_pick_goods_index, 2].tolist(),
         '仓库': seller.iloc[final_pick_goods_index, 4].tolist(), '分配货物数量': final_pick_goods,
         '对应意向顺序': final_pick_goods_satisfy})
    output = pd.concat([output, output_append], ignore_index=True)

    seller_SR = seller[seller['品种'] == 'SR']
    return final_pick_goods_index, final_pick_goods, final_pick_goods_satisfy
  • 写回答

2条回答 默认 最新

  • PythonJavaC++go 2020-12-25 22:53
    关注

    学习pandas的操作语法

    而不是自己思考 普通python语法

     

    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路