- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does the pandas series.equals() method handle the null values?
It is very common to have missing values in a series object, and if you want to compare that type of series objects then the ordinary comparison does not work because nan != nan, In that case, we can use the equals() method. The equals() method considers Nan’s in the same location to be equal.
The fundamental operation of the pandas series.equals() method is used to compare two series for equality. it returns True if the two series have the same elements and shape, and returns False if the two series are unequal.
Example 1
In the following example, two series objects series1 and series2 are applied to the equals() method to compare equality.
# importing pandas package import pandas as pd import numpy as np # create pandas Series1 series1 = pd.Series([67, 18, np.nan, 50, 39]) print("First series object:",series1) # create pandas Series2 series2 = pd.Series([67, 18, np.nan, 50, 39]) print("second series object:",series2) result = series1.equals(series2) print("Result:", result)
Explanation
Since the series objects are exactly similar and there is a nan value present in two series objects at the same location.
Output
The output is mentioned below −
First series object: 0 67.0 1 18.0 2 NaN 3 50.0 4 39.0 dtype: float64 Second series object: 0 67.0 1 18.0 2 NaN 3 50.0 4 39.0 dtype: float64 Result: True
Here the two series objects are exactly similar with the same data type, and the equals() method returns True as a result. Also, you can see that NaNs are considered equal if they occur at the same location.
Example 2
In the following example, two series objects series1 and series2 are verified for equality by applying the equals() method.
import pandas as pd import numpy as np # create pandas Series1 series1 = pd.Series([92, 68, 65, np.nan]) print("First series object:",series1) # create pandas Series2 series2 = pd.Series(['92', '68', '65', np.nan]) print("second series object:",series2) result = series1.equals(series2) print("Result :", result)
Explanation
Here the series objects are similar but the data types of the elements are different.
Output
The output is given below −
First series object: 0 92.0 1 68.0 2 65.0 3 NaN dtype: float64 Second series object: 0 92 1 68 2 65 3 NaN dtype: object Result: False
For the above given example, the equality function equals() returns False. Because the data types of the elements in the two series objects are not the same.
- Related Articles
- What is the basic operation of the series.equals() method in pandas?
- How to handle the null values while comparing the two series objects using series.eq() method?
- How to handle Null values while working with JDBC?
- How does the pandas series.ffill() method work?
- How does the pandas series.expanding() method work?
- How does the pandas series.first_valid_index() method work?
- How does the pandas Series idxmax() method work?
- How does the pandas Series idxmin() method work?
- How does the series.copy() method work in Pandas?
- How does the series.corr() method work in pandas?
- How does the series.cummax() method work in Pandas?
- How does the series.cumprod() method work in Pandas?
- How does the series.cumsum() method work in Pandas?
- How does the pandas series.divmod() method work?\n
- How to concat Values in MySQL Query and to handle Null values as well?
