首页 > 解决方案 > 通过对列进行分组来连接连续的行

问题描述

我正在尝试在 Python 中ProdID基于 ProdCategory 进行连接。我只需要最后两列MainProdConcatMainProdConcat_PCOnly.

让我知道它是否可能

OrderN0 ProdID  ProdCategory    ItemNo  ProdType    MainItem MainProdConcat MainProdConcat_PConly
123334  1   PC  100 Main    100 1,2,3,4,5,6 1,2,3,4
123334  2   PC  110 Option  100 1,2,3,4,5,6 1,2,3,4
123334  3   PC  120 Option  100 1,2,3,4,5,6 1,2,3,4
123334  4   PC  130 Option  100 1,2,3,4,5,6 1,2,3,4
123334  5   Accessories 140 Option  100 1,2,3,4,5,6 
123334  6   Accessories 150 Option  100 1,2,3,4,5,6 
123334  7   PC  200 Main    200 7,8,9,10,11 7,8,9,10
123334  8   PC  210 Option  200 7,8,9,10,11 7,8,9,10
123334  9   PC  220 Option  200 7,8,9,10,11 7,8,9,10
123334  10  PC  240 Option  200 7,8,9,10,11 7,8,9,10
123334  11  Accessories 260 Option  200 7,8,9,10,11 

for index, row in df_OrderNo_WithBase.iterrows(): 
      orderid = row['Legacy Sales Order Identifier'] 
      dealid = row['Deal ID'] 
      df_Master.loc[(df_Master['OrderNo'] == orderid ) & (df_Master['Deal ID'] == dealid)),'ProductConcatMain'] = df_Master[(df_Master['OrderNo'] == orderid) & (df_Master['Deal ID'] == dealid) ]['ProdID'].str.cat(sep=',') 

标签: pythonpandas

解决方案


input = '''
OrderN0 ProdID  ProdCategory    ItemNo  ProdType    MainItem MainProdConcat MainProdConcat_PConly
123334  1   PC  100 Main    100 1,2,3,4,5,6 1,2,3,4
123334  2   PC  110 Option  100 1,2,3,4,5,6 1,2,3,4
123334  3   PC  120 Option  100 1,2,3,4,5,6 1,2,3,4
123334  4   PC  130 Option  100 1,2,3,4,5,6 1,2,3,4
123334  5   Accessories 140 Option  100 1,2,3,4,5,6 
123334  6   Accessories 150 Option  100 1,2,3,4,5,6 
123334  7   PC  200 Main    200 7,8,9,10,11 7,8,9,10
123334  8   PC  210 Option  200 7,8,9,10,11 7,8,9,10
123334  9   PC  220 Option  200 7,8,9,10,11 7,8,9,10
123334  10  PC  240 Option  200 7,8,9,10,11 7,8,9,10
123334  11  Accessories 260 Option  200 7,8,9,10,11'''

from itertools import groupby

table = [x.split() for x in input.split("\n")]
heading = table[1]
data = [dict(zip(heading, x)) for x in table[2:]]

for x,y in groupby(data, key=lambda x: x['MainItem']):
    y = list(y)
    MainProdConcat = ','.join([z['ProdID'] for z in y])
    MainProdConcat_PConly = ','.join([z['ProdID'] for z in y if z['ProdCategory'] == 'PC'])
    for t in y:
        print t['ProdID'], MainProdConcat,
        if t['ProdCategory'] == 'PC':
            print MainProdConcat_PConly
        else:
            print

输出:

1 1,2,3,4,5,6 1,2,3,4
2 1,2,3,4,5,6 1,2,3,4
3 1,2,3,4,5,6 1,2,3,4
4 1,2,3,4,5,6 1,2,3,4
5 1,2,3,4,5,6
6 1,2,3,4,5,6
7 7,8,9,10,11 7,8,9,10
8 7,8,9,10,11 7,8,9,10
9 7,8,9,10,11 7,8,9,10
10 7,8,9,10,11 7,8,9,10
11 7,8,9,10,11

推荐阅读