比较两列并根据参考列的顺序输出新列

时间:2019-06-24 14:58:55

标签: excel python-3.x

我正在尝试比较具有相同ID(只是顺序不同)的两列(列表)。我想引用第一列的顺序,将其与下一列进行比较,然后根据新列(或列表)中的第一列顺序重新格式化第二列的顺序。从那里,我可以拉出与第一列顺序匹配的相应列(价格,人口统计信息等)。

输入:

第一列(参考列):

12321
12323
324214
32313452
1232132
fs2421
sfasrfas
asfasd

第二列(需要重新排序):

12321
sfasrfas
12323
324214
1232132
fs2421
asfasd
32313452

我尝试用Python编写一个for循环,每个列ID都有两个单独的列表,而excel中的Index / Match却似乎只能输出“匹配”的ID。

Excel

=INDEX($A$2:$A$589,MATCH(C2,$A$2:$A$589,0),2)

Python

## setting empty list and extract only matched values from both lists made above ##
matched_IDs = []
unique_IDs = []
for Part_No in updated_2_list:
    if Part_No in updated_1_list:
        matched_IDs.append(Part_No)
    elif Part_No not in updated_2_list:
        unique_IDs.append(Part_No)
print(matched_IDs)
#len(matched_IDs)
len(matched_IDs)

我希望匹配新列(或列表)中第一列的顺序。 输出:

第三列(第二列之后的新列已重新排序)

12321
12323
324214
32313452
1232132
fs2421
sfasrfas
asfasd

1 个答案:

答案 0 :(得分:0)

您的意思是这样的:

=INDEX(C:C,MATCH(A1,C:C,0))

enter image description here