Different Ways to Compare Two Columns From Two Different Files Using Pandas

Finding common values between two columns

Indhumathy Chelliah
Women in Technology

--

Photo by Maksim Goncharenok: https://www.pexels.com/photo/two-people-holding-pineapple-fruits-against-a-multicolored-wall-4412925/

During file processing, we might have encountered many situations to compare two columns from two different files. In this article, let’s look at different methods to find common values between two columns in two different files using pandas dataframe.

Let’s find the matching values between two columns in two different files using pandas.

First, Convert the excel file to pandas dataframe.

df1=pd.read_excel("example1.xlsx")
df2=pd.read_excel("example3.xlsx")

Let’s compare the “Number” column from the “example1” with the “Num” column in “example3”

Method 1: Using the set intersection method

Set intersection method — will find the unique common values between two sets.

common_values_1 = set(df1['Number'].values).intersection(set(df2['Num'].values))

Result: {1, 2, 3, 4, 5}

--

--