Anders Larsen
Anders Larsen

Reputation: 1

Compare two columns in sheet with Python and print matches

I have a group of keywords in two different columns in a Google Sheets. How do I compare and print the matches:

Example:

Column A 
Row 1 Hi
Row 2 Hallo
Row 3 Bye

Column B
Row 1 Hi
Row 2 No
Row 3 Hallo

Print:

Hi
Hallo

Or is it possible directly in sheets? Thanks! :)

Upvotes: 0

Views: 3790

Answers (3)

Inon Peled
Inon Peled

Reputation: 711

You can do it all in Google Sheets. Suppose that the first set of values is in column A and the second set of values is in column B. Then:

  1. In a new column, paste and drag this formula: =IF(ISERROR(MATCH(B1,A:A,0)),"Not found","Found").

  2. Sort the new column, and manually copy the range of values in column B that are adjacent to "Found" in the new column.

There are also other ways to do it in Google Sheets, this is just one of them.

Upvotes: 0

maria
maria

Reputation: 534

If you can save your file in .csv format, you can use pandas library.

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)))

Upvotes: 1

Sean Lee
Sean Lee

Reputation: 46

# 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)

Upvotes: 1

Related Questions