2.4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Check if Multiple Columns are Equal

Pandas: How to Check if Multiple Columns are Equal

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories