简体   繁体   English

根据熊猫数据框python中终端负载的节点/标签分配

[英]Nodal/Label assignment according to the load at a terminal in pandas data frame python

Input data frame with requirement for boarding in a transport allocation setting.在运输分配设置中输入具有登机要求的数据框。

import pandas as pd

data1 = {
        'N_Id': [0,1,2,2,2,2,4,5,5,5,10,10,11,11,13,13,13,13],
        'N_Name': ['Destiny','N_Area','Pstation','Pstation','Pstation','Pstation','A_Area','T_depot','T_depot','T_depot','B_colony','B_colony','c_colony','c_colony','Z_colony','Z_colony','Z_colony','Z_colony'],
        'Geocode': ['9.994798,0.206728','2.994798,0.206728','2.989385,0.201941','2.989385,0.201941','2.989385,0.201941','2.989385,0.201941','3.006515,0.247101','3.000568,0.256468','3.000568,0.256468','3.000568,0.256468','3.049031,0.167278','3.049031,0.167278','4.049031,0.167278','4.049031,0.167278','5.2,0.7','5.2,0.7','5.2,0.7','5.2,0.7'],
        'pcode': ['None','M023','L123','M0232','L1234','K0324','M0137','M01368','M01369','K07249','M01375','F04509','F04609','F04610','F1','F2','F3','F4']
    }
    
df1 = pd.DataFrame.from_dict(data1)

output输出

df1
Out[45]: 
    N_Id    N_Name            Geocode   pcode
0      0   Destiny  9.994798,0.206728    None
1      1    N_Area  2.994798,0.206728    M023
2      2  Pstation  2.989385,0.201941    L123
3      2  Pstation  2.989385,0.201941   M0232
4      2  Pstation  2.989385,0.201941   L1234
5      2  Pstation  2.989385,0.201941   K0324
6      4    A_Area  3.006515,0.247101   M0137
7      5   T_depot  3.000568,0.256468  M01368
8      5   T_depot  3.000568,0.256468  M01369
9      5   T_depot  3.000568,0.256468  K07249
10    10  B_colony  3.049031,0.167278  M01375
11    10  B_colony  3.049031,0.167278  F04509
12    11  c_colony  4.049031,0.167278  F04609
13    11  c_colony  4.049031,0.167278  F04610
14    13  Z_colony            5.2,0.7      F1
15    13  Z_colony            5.2,0.7      F2
16    13  Z_colony            5.2,0.7      F3
17    13  Z_colony            5.2,0.7      F4

second data frame with a route plan/to load according to the Terminal/Nodal load.第二个数据框,带有路线计划/根据终端/节点负载进行加载。

data2= {
            'BusID': ['V1','V1','V1','V4','V4','V4','V5','V5','V5','V0','v100'],
            'Tcap': [4,4,4,8,8,8,12,12,12,12,8],
            'Terminal_Load':[1,2,1,2,1,1,2,1,1,0,4],
            'N_Id': [1,2,5,2,5,10,11,4,10,0,13],
            'N_Name': ['N_Area','Pstation','T_depot','Pstation','T_depot','B_colony','c_colony','A_Area','B_colony','Destiny','Z_colony'],
            
        }
        
df2 = pd.DataFrame.from_dict(data2)

output of the second data frame.第二个数据帧的输出。

df2
Out[46]: 
   BusID  Tcap  Terminal_Load  N_Id    N_Name
0     V1     4              1     1    N_Area
1     V1     4              2     2  Pstation
2     V1     4              1     5   T_depot
3     V4     8              2     2  Pstation
4     V4     8              1     5   T_depot
5     V4     8              1    10  B_colony
6     V5    12              2    11  c_colony
7     V5    12              1     4    A_Area
8     V5    12              1    10  B_colony
9     V0    12              0     0   Destiny
10  v100     8              4    13  Z_colony

required Dataframe format..所需的数据框格式..

data3 = {
            'BusID': ['V1','V1','V1','V4','V4','V4','V5','V5','V5','V0','v100'],
            'Tcap': [4,4,4,8,8,8,12,12,12,12,8],
            'Terminal_Load':[1,2,1,2,1,1,2,1,1,0,4],
            'N_Id': [1,2,5,2,5,10,11,4,10,0,13],
            'N_Name': ['N_Area','Pstation','T_depot','Pstation','T_depot','B_colony','c_colony','A_Area','B_colony','Destiny','Z_colony'],
            'Pcode': ['M023','L123,M0232','M01368','L1234,K0324','M01369','M01375','F04609,F04610','M0137','F04509','','F1,F2,F3,F4'],
        }

df3 = pd.DataFrame.from_dict(data3)

required output...需要的输出...

df3
Out[47]: 
   BusID  Tcap  Terminal_Load  N_Id    N_Name          Pcode
0     V1     4              1     1    N_Area           M023
1     V1     4              2     2  Pstation     L123,M0232
2     V1     4              1     5   T_depot         M01368
3     V4     8              2     2  Pstation    L1234,K0324
4     V4     8              1     5   T_depot         M01369
5     V4     8              1    10  B_colony         M01375
6     V5    12              2    11  c_colony  F04609,F04610
7     V5    12              1     4    A_Area          M0137
8     V5    12              1    10  B_colony         F04509
9     V0    12              0     0   Destiny               
10  v100     8              4    13  Z_colony    F1,F2,F3,F4

The required Dataframe is the combination of both the first to dataframe, I need to assign the PCode based on the requirement Terminal Load.所需的数据帧是第一个数据帧的组合,我需要根据终端负载的要求分配 PCode。 The load at a terminal is on the Terminal_Load value, for example at nodal ID 1 the load is 1 so 1 person is listed in the pcode, 2nd row terminal load is 2 and the nodal ID is 2 hence two person are loaded at Pcode.终端的负载在 Terminal_Load 值上,例如在节点 ID 1 处,负载为 1,因此 pcode 中列出了 1 人,第二行终端负载为 2,节点 ID 为 2,因此在 Pcode 处加载了两个人。 Terminal load 0 at the 0 node.终端在 0 节点加载 0。 requesting a pandas dataframe solution or any Non Pandas implementation just guessing any labelled pop up methods..or any algorithm to capacity and allocate.. and after allocating remove the label from the queue.请求 pandas 数据框解决方案或任何非 Pandas 实现只是猜测任何标记的弹出方法..或任何算法来容量和分配..并在分配后从队列中删除标签。

First aggregate list like previous solution and add new column to df2 with aggregate lists:像以前的解决方案一样首先聚合列表,并使用聚合列表向df2添加新列:

s = (df1.groupby(['N_Id','N_Name'])['pcode']
        .agg(list)
        .rename('Pcode'))

df = df2.join(s, on=['N_Id','N_Name'])

Then need split lists per groups (same per groups, so selected first list by iat[0] ) by Terminal_Load values like linked solution - only is necessary cumulative sum and starting by 0 :然后需要通过Terminal_Load值(如链接解决方案)按组拆分列表(每组相同,因此由iat[0]选择第一个列表) - 只有必要的累积总和并从0开始:

def f(x):
    indices = [0] + x['Terminal_Load'].cumsum().tolist()
    s = x['Pcode'].iat[0]
    #https://stackoverflow.com/a/10851479/2901002
    x['Pcode'] = [','.join(s[indices[i]:indices[i+1]]) for i in range(len(indices)-1)]
    return x

df = df.groupby(['N_Id','N_Name']).apply(f)
print (df)
   BusID  Tcap  Terminal_Load  N_Id    N_Name          Pcode
0     V1     4              1     1    N_Area           M023
1     V1     4              2     2  Pstation     L123,M0232
2     V1     4              1     5   T_depot         M01368
3     V4     8              2     2  Pstation    L1234,K0324
4     V4     8              1     5   T_depot         M01369
5     V4     8              1    10  B_colony         M01375
6     V5    12              2    11  c_colony  F04609,F04610
7     V5    12              1     4    A_Area          M0137
8     V5    12              1    10  B_colony         F04509
9     V0    12              0     0   Destiny               
10  v100     8              4    13  Z_colony    F1,F2,F3,F4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM