Comparing two Pandas series and printing the the difference

In this program, we will compare two Pandas series and will print the differences in the series. By difference, we mean that the index positions at which the elements did not match.


Step 1: Define two Pandas series, s1 and s2.
Step 2: Compare the series using compare() function in the Pandas series.
Step 3: Print their difference.

Example Code

import pandas as pd

s1 = pd.Series([10,20,30,40,50,60])
s2 = pd.Series([10,30,30,40,55,60])

print("S1:\n", s1)
print("\nS2:\n", s2)

difference =
print("\nDifference between the series: \n",difference)


0    10
1    20
2    30
3    40
4    50
5    60
dtype: int64

0    10
1    30
2    30
3    40
4    55
5    60
dtype: int64

Difference between the series:
    self  other
1  20.0   30.0
4  50.0   55.0


In the above output, there are two columns in the difference output. One is 'self' and beside it is 'other'. The self refers to s1 series whereas the 'other' refers to s2 series.

Updated on: 16-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started