使用Python和打印匹配比较工作表中的两列

问题描述 投票:0回答:3

我在Google表格的两个不同列中有一组关键字。如何比较和打印匹配项:

例:

Column A 
Row 1 Hi
Row 2 Hallo
Row 3 Bye

Column B
Row 1 Hi
Row 2 No
Row 3 Hallo

打印:

Hi
Hallo

或者可以直接在床单上?谢谢! :)

python
3个回答
0
投票
# read column1 to list_1. there are some libs could help you.
list_1 = []
# read column2 to list_2
list_2 = []

# calc the result from list_1 and list_2
res = [x for x in list_1 for y in list_2 if x == y]
print(res)

0
投票

如果您可以以.csv格式保存文件,则可以使用pandas库。

import pandas as pd

df = pd.read_csv('filename.csv')

column_1_values = set(df['A'].unique())
column_2_values = set(df['B'].unique())
matches = column_1_values.intersection(column_2_values)

print('\n'.join(map(str, matches)))

0
投票

您可以在Google表格中完成所有操作。假设第一组值在A列中,第二组值在B列中。然后:

  1. 在新列中,粘贴并拖动此公式:=IF(ISERROR(MATCH(B1,A:A,0)),"Not found","Found")
  2. 对新列进行排序,并手动复制B列中与新列中“已找到”相邻的值范围。

在Google表格中还有其他方法可以做到这一点,这只是其中之一。

© www.soinside.com 2019 - 2024. All rights reserved.