python - 通过对列进行分组来连接连续的行
问题描述
我正在尝试在 Python 中ProdID
基于 ProdCategory 进行连接。我只需要最后两列MainProdConcat
和MainProdConcat_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=',')
解决方案
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
推荐阅读
- tomcat - 基于 Spring 的 GemFire 客户端可以处理的同时请求数
- python - Peewee ORM -- get all rows EXCEPT those shared in another query
- angular - Angular Mat-Slider 自定义
- ms-access - 如何从表中删除“中间过程”数据?
- java - 如何将 Log4J 输出复制到字符串?
- javascript - Javascript For Loop迭代多个输入数量限制
- javascript - 打开多个模态时的 twitter bootstrap / Inspinia 模态错误
- android - 可在 MaterialButton android 上绘制的选择器
- python - 如何在 python 中下载整个 OneDrive 文件夹
- android - 在数据绑定中以额外意图开始活动的最佳实践是什么?