Pandas: How to Check if Multiple Columns are Equal


You can use the following methods to check if multiple columns are equal in pandas:

Method 1: Check if All Columns Are Equal

df['matching'] = df.eq(df.iloc[:, 0], axis=0).all(1)

Method 2: Check if Specific Columns Are Equal

df['matching'] = df.apply(lambda x: x.col1 == x.col3 == x.col4, axis=1)

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'A': [4, 0, 3, 3, 6, 8, 7],
                   'B': [4, 2, 3, 5, 6, 4, 7],
                   'C': [4, 0, 3, 3, 5, 10, 7],
                   'D': [4, 0, 3, 3, 3, 8, 7]})

#view DataFrame
print(df)

   A  B   C  D
0  4  4   4  4
1  0  2   0  0
2  3  3   3  3
3  3  5   3  3
4  6  6   5  3
5  8  4  10  8
6  7  7   7  7

Example 1: Check if All Columns Are Equal

We can use the following syntax to check if the value in every column in the DataFrame is equal for each row:

#create new column that checks if all columns match in each row
df['matching'] = df.eq(df.iloc[:, 0], axis=0).all(1)

#view updated DataFrame
print(df)

   A  B   C  D  matching
0  4  4   4  4      True
1  0  2   0  0     False
2  3  3   3  3      True
3  3  5   3  3     False
4  6  6   5  3     False
5  8  4  10  8     False
6  7  7   7  7      True

If the value in each column is equal, then the matching column returns True.

Otherwise, it returns False.

Note that you can convert True and False values to 1 and 0 by using astype(int) as follows:

#create new column that checks if all columns match in each row
df['matching'] = df.eq(df.iloc[:, 0], axis=0).all(1).astype(int)

#view updated DataFrame
print(df)

   A  B   C  D  matching
0  4  4   4  4         1
1  0  2   0  0         0
2  3  3   3  3         1
3  3  5   3  3         0
4  6  6   5  3         0
5  8  4  10  8         0
6  7  7   7  7         1

Example 2: Check if Specific Columns Are Equal

We can use the following syntax to check if the value in columns A, C, and D in the DataFrame are equal for each row:

#create new column that checks if values in columns A, C, and D are equal
df['matching'] = df.apply(lambda x: x.A == x.C == x.D, axis=1)

#view updated DataFrame
print(df)

   A  B   C  D  matching
0  4  4   4  4      True
1  0  2   0  0      True
2  3  3   3  3      True
3  3  5   3  3      True
4  6  6   5  3     False
5  8  4  10  8     False
6  7  7   7  7      True

If the value in columns A, C, and D are equal, then the matching column returns True.

Otherwise, it returns False.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Rename Columns in Pandas
How to Add a Column to a Pandas DataFrame
How to Change the Order of Columns in Pandas DataFrame

Leave a Reply

Your email address will not be published.