代码之家  ›  专栏  ›  技术社区  ›  ManOnTheMoon

多进程循环(python)

  •  0
  • ManOnTheMoon  · 技术社区  · 3 年前

    我有一个数据帧,我正在尝试使用多进程来更有效地使用可用的核心进行循环。有一个例子 here 然而,我不知道“pool”是如何应用的。

    示例df代码:

    df = pd.DataFrame({
        'mac':['type_a','type_a','type_a','type_a','type_a','type_b','type_b','type_b','type_b','type_b'],
        'con':['a','a','a','c','b','a','a','b','a','c'],
        'result':[1,1,2,2,3,1,1,3,1,2],
    })
    
    MAX_NUMBER = 3 
    for j in range(MAX_NUMBER):
        
        i = j + 1 
        aft = f"mc{i}_aft"
        bef = f"mc{i}_bef"
        
        holder1 = ['con', 'mac', 'result']
        holder2 = ['con', 'mac']
        df['add'] = (df.groupby(holder1).cumcount() + 1)
        df[aft] = df['add'].loc[df.result == i]
    
        df = df.groupby(holder2, group_keys=False).apply(lambda x: x.fillna(method='ffill').fillna(0))
    
        df = df.drop(['add'], axis=1)
        df.fillna(0, inplace=True)
        df[aft] = df[aft].astype('int')
        
        df[bef] = df.groupby(holder2)[aft].shift(1)
        df.fillna(0, inplace=True)
        df[bef] = df[bef].astype('int')
    

    示例df:

    ╔═══╦════════╦═════╦════════╗
    ║   ║  mac   ║ con ║ result ║
    ╠═══╬════════╬═════╬════════╣
    ║ 0 ║ type_a ║ a   ║      1 ║
    ║ 1 ║ type_a ║ a   ║      1 ║
    ║ 2 ║ type_a ║ a   ║      2 ║
    ║ 3 ║ type_a ║ c   ║      2 ║
    ║ 4 ║ type_a ║ b   ║      3 ║
    ║ 5 ║ type_b ║ a   ║      1 ║
    ║ 6 ║ type_b ║ a   ║      1 ║
    ║ 7 ║ type_b ║ b   ║      3 ║
    ║ 8 ║ type_b ║ a   ║      1 ║
    ║ 9 ║ type_b ║ c   ║      2 ║
    ╚═══╩════════╩═════╩════════╝
    

    样本输出:

    ╔═══╦════════╦═════╦════════╦═════════╦═════════╦═════════╦═════════╦═════════╦═════════╗
    ║   ║  mac   ║ con ║ result ║ mc1_aft ║ mc1_bef ║ mc2_aft ║ mc2_bef ║ mc3_aft ║ mc3_bef ║
    ╠═══╬════════╬═════╬════════╬═════════╬═════════╬═════════╬═════════╬═════════╬═════════╣
    ║ 0 ║ type_a ║ a   ║      1 ║       1 ║       0 ║       0 ║       0 ║       0 ║       0 ║
    ║ 1 ║ type_a ║ a   ║      1 ║       2 ║       1 ║       0 ║       0 ║       0 ║       0 ║
    ║ 2 ║ type_a ║ a   ║      2 ║       2 ║       2 ║       1 ║       0 ║       0 ║       0 ║
    ║ 3 ║ type_a ║ c   ║      2 ║       0 ║       0 ║       1 ║       0 ║       0 ║       0 ║
    ║ 4 ║ type_a ║ b   ║      3 ║       0 ║       0 ║       0 ║       0 ║       1 ║       0 ║
    ║ 5 ║ type_b ║ a   ║      1 ║       1 ║       0 ║       0 ║       0 ║       0 ║       0 ║
    ║ 6 ║ type_b ║ a   ║      1 ║       2 ║       1 ║       0 ║       0 ║       0 ║       0 ║
    ║ 7 ║ type_b ║ b   ║      3 ║       0 ║       0 ║       0 ║       0 ║       1 ║       0 ║
    ║ 8 ║ type_b ║ a   ║      1 ║       3 ║       2 ║       0 ║       0 ║       0 ║       0 ║
    ║ 9 ║ type_b ║ c   ║      2 ║       0 ║       0 ║       1 ║       0 ║       0 ║       0 ║
    ╚═══╩════════╩═════╩════════╩═════════╩═════════╩═════════╩═════════╩═════════╩═════════╝
    

    ps.ectual df要大得多,涉及更多的变量和循环,因此,尝试充分利用可用的内核来节省处理时间。循环按测试前(bef)和测试后(aft)的结果/累计数分组。

    恳请

    0 回复  |  直到 3 年前