Find Differences Between Two Columns of pandas DataFrame in Python (3 Examples)

 

In this Python tutorial you’ll learn how to compare two columns of a pandas DataFrame.

The tutorial will consist of three examples for the comparison of two columns of a pandas DataFrame. To be more specific, the content of the article looks as follows:

Here’s how to do it…

 

Example Data & Add-On Libraries

We first have to load the pandas library:

import pandas as pd                              # Load pandas

Next, we’ll also need to construct some example data.

data = pd.DataFrame({'x1':[1, 3, 2, 4, 7, 5],    # Create pandas DataFrame
                     'x2':['a', 'b', 'c', 'd', 'e', 'f'],
                     'x3':range(1, 7)})
print(data)                                      # Print pandas DataFrame

 

table 1 DataFrame find differences between two columns pandas dataframe python

 

Table 1 reveals the structure of our exemplifying data: It is a pandas DataFrame constructed of six rows and three columns.

The two columns x1 and x3 look similar, so let’s compare them in Python!

 

Example 1: Check If All Elements in Two pandas DataFrame Columns are Equal

In Example 1, I’ll illustrate how to test whether each element of a first column is equal to each element of a second column.

For this task, we can use the equals function as shown below:

print(data['x1'].equals(data['x3']))             # Apply equals function
# False

After executing the previous Python code, the logical indicator False is returned, i.e. the columns x1 and x3 are not identical.

 

Example 2: Check which Elements in Two pandas DataFrame Columns are Equal

As shown in Example 1, there are differences between the columns x1 and x3. Let’s check if at least some of the elements in these columns are the same.

To achieve this, we can use the == operator as shown in the following Python syntax:

print(data['x1'] == data['x3'])                  # Apply logical operator
# 0     True
# 1    False
# 2    False
# 3     True
# 4    False
# 5    False
# dtype: bool

The result of the previous code are multiple logical indicators that correspond to each row of our pandas DataFrame.

In case a logical operator is True, toe values in the columns x1 and x3 in this row are equal.

Note that this method takes the ordering of the values in our pandas DataFrame rows into account.

 

Example 3: Check which Elements in First pandas DataFrame Column are Contained in Second

In this example, I’ll show how to check which of the values in a pandas DataFrame column are also contained in another column – no matter in which order the values are appearing.

To find this out, we can use the isin function as shown below:

print(data['x1'].isin(data['x3']))               # Apply isin function
# 0     True
# 1     True
# 2     True
# 3     True
# 4    False
# 5     True
# Name: x1, dtype: bool

Once again, we have returned several logical indicators. As you can see, only the value in the column x1 at the index position 4 is not contained in the variable x3.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. In the video, I’m explaining the examples of this page.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might want to read some of the related articles on my website. I have released numerous other tutorials already.

 

To summarize: This page has demonstrated how to compare two columns of a pandas DataFrame and find differences in the Python programming language. In case you have further questions, please let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top