跟踪两个数据集(或矩阵)之间的数据移动

2024-09-30 18:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据集,例如7月和8月的数据,每个数字表示下订单的数量。 我想比较一下这两组数据,找出它们之间的差异。 主要有三个区别
a) =订单取消(数字存在于左侧数据集中,而不存在于右侧)
b) =新顺序(数字存在于右侧数据集中,但不存在于左侧)
c) =延期订单(两个数据集中都有编号(但右数据集中的编号在后一个月M0存在)
d) =前置(为了简化,我们假设不会发生这种情况)

     July                       August              
TypeM09 M10 M11 M12     TypeM09 M10 M11 M12
A   0   1   2   0       A   0   1   2   0
B   1   0   0   0       B   0   0   1   0
C   1   2   0   0       C   0   1   0   0
D   1   2   2   0       D   1   0   0   2

注意-在上面的D类型中,数字2在M10和M11的左侧数据集中出现两次,在M12的右侧数据集中出现一次。由于程序从左到右检查数据,一旦程序发现M12(在左数据集中)中存在M10(在右数据集中),M11(在左数据集中)中的数字实际上被取消,因为在右数据集中没有更多的“自由”数字可供匹配

程序应该是这样的

Type    M09                   M10                     M11            M12
A       -                     no change no change     -              -
B       postponement to M11   -                       -              -
C       postponement to M10   cancellation  -   -
D       no change             postponement to M11      cancellation  -

作为一个输出,我想实现如下目标:

Type    Detail          Previous month  New month
x       Postponement    M07             M11
x       Postponement    M08             M12
y       Cancellation    M08             -
z       New order       -               M12

Tags: to数据no订单程序type数字change
1条回答
网友
1楼 · 发布于 2024-09-30 18:24:25

我仍然没有什么疑问,所以我只能产生中间输出。。 对于A、B、C、D型所提到的示例,请添加更多的说明和所需的输出

from collections import defaultdict
data_2dlist_1 = [
    ['Type', 'M09' ,'M10', 'M11', 'M12'],
    ['A' ,  0 , 1 , 2 , 0] ,     
    ['B' ,  1 , 0 , 0 , 0] ,     
    ['C' ,  1 , 2 , 0 , 0] ,     
    ['D' ,  1 , 2 , 2 , 0] ]

data_2dlist_2 =[
['Type', 'M09' ,'M10', 'M11', 'M12'],
['A', 0,   1,   2,   0],
['B', 0,   0,   1,   0],
['C', 0,   1,   0,   0],
['D', 1,   0,   0,   2]]

output_2d = [['Type', 'M09' ,'M10', 'M11', 'M12']]
final_output_2d = [['Type','Detail','Previous month'  ,'New month']]


for d1, d2 in zip(data_2dlist_1[1:],data_2dlist_2[1:]):
    #print(d1,d2)
    output_2d.append([d1[0]]) #new_type row creation
    looked = defaultdict()

    for i in range(1,len(d1),1):

        if d1[i] == 0: 
            output_2d[-1].append('-')

        elif d1[i] == d2[i]:
            output_2d[-1].append('no change')

        elif d1[i] != 0: 
            start = i+1
            if d1[i] in looked:
                start = looked[d1[i]] + 1
            try:
                found_at = d2.index(d1[i],start)
                output_2d[-1].append('postponement to '+ str(data_2dlist_2[0][found_at]))
                looked[d1[i]] = found_at

            except ValueError: #not found 
                output_2d[-1].append('cancellation')

        elif d2[i] not in looked: #and d1[i] == 0
            output_2d[-1].append('new order')

print(output_2d)

'''       

[['Type', 'M09', 'M10', 'M11', 'M12'],
['A', '-', 'no change', 'no change', '-'],
['B', 'postponement to M11', '-', '-', '-'],
['C', 'postponement to M10', 'cancellation', '-', '-'],
['D', 'no change', 'postponement to M12', 'cancellation', '-']]

'''

相关问题 更多 >