【问题标题】:Compare two columns in sheet with Python and print matches使用 Python 比较工作表中的两列并打印匹配项
【发布时间】:2019-08-22 14:23:21
【问题描述】:

我在 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


【解决方案1】:

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

  1. 在新列中,粘贴并拖动以下公式:=IF(ISERROR(MATCH(B1,A:A,0)),"Not found","Found")

  2. 对新列进行排序,并在新列中手动复制 B 列中与“找到”相邻的值范围。

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

【讨论】:

    【解决方案2】:

    如果您可以将文件保存为 .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)))
    

    【讨论】:

    • 我会试试你的解决方案,玛丽亚! :) 谢谢。
    【解决方案3】:
    # 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)
    

    【讨论】:

    • 虽然此命令可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。 How to Answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 2023-02-25
    • 2016-12-23
    • 2020-11-24
    • 2010-11-26
    • 2016-07-30
    相关资源
    最近更新 更多