- 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 to handle the null values while comparing the two series objects using series.eq() method?
The Pandas series.eq() method is used to compare every element of a given series with a passed parameter (other series object or a scalar value). It will return True for every element which is equal to the element in the other series object (passed series object).
The output of the eq() method is a series with boolean values and it performs an element-wise comparison operation which is nothing but caller series = other series. In the resultant series, the True value indicates the equivalent value in the other series object as well as, the False value indicates an unequal value.
Handling of Null values using this eq() method is a very easy process by providing the fill_value parameter. By default, this parameter takes None for missing values.
Example 1
In the following example, we will see how the eq() method handles missing values.
# importing packages import pandas as pd import numpy as np #create series sr = pd.Series([np.nan, 76, 20, 51, np.nan, 26, 83, np.nan, 18]) print(sr) # compare elements with a scalar value 20 and replacing NAN values with 20 result = sr.eq(20, fill_value=20) print(result)
Explanation
Initially, we have created a pandas Series by using a list of integers and some Nan values. After that, we compared the series object sr with a value 20 by using the eq() method, and we applied a value 20 to the fill_value parameter() to handle the missing.
Output
The output is given below −
0 NaN 1 76.0 2 20.0 3 51.0 4 NaN 5 26.0 6 83.0 7 NaN 8 18.0 dtype: float64 0 True 1 False 2 True 3 False 4 True 5 False 6 False 7 True 8 False dtype: bool
Initially, the eq() method replaces the missing value by the specified fill_value which is 20. After that, it will perform the comparison operation between the called series object and the passed object.
Example 2
Same as in the previous example, here the eq() method will compare two series objects and replace missing values with a value 5 which is specified by the fill_value parameter.
# importing packages import pandas as pd import numpy as np #create series sr1 = pd.Series([26, np.nan, 18, np.nan, 94, 71, 5, np.nan, 68, 54, 88, 7, np.nan]) print(sr1) sr2 = pd.Series([26, 29, np.nan, 11, 82, 93, np.nan, 7, 68, 29, 88, 87, np.nan]) print(sr2) # compare two series objects result = sr1.eq(sr2, fill_value=5) print(result)
Output
The output is as follows −
0 26.0 1 NaN 2 18.0 3 NaN 4 94.0 5 71.0 6 5.0 7 NaN 8 68.0 9 54.0 10 88.0 11 7.0 12 NaN dtype: float64 0 26.0 1 29.0 2 NaN 3 11.0 4 82.0 5 93.0 6 NaN 7 7.0 8 68.0 9 29.0 10 88.0 11 87.0 12 NaN dtype: float64 0 True 1 False 2 False 3 False 4 False 5 False 6 True 7 False 8 True 9 False 10 True 11 False 12 False dtype: bool
The above output shows that the eq() method returns a new series object with boolean values. The value True was returned wherever values in two series objects are Equal. Also, we can see Null values were replaced by 5 and the comparison was made using that value 5.
- Related Articles
- How to handle Null values while working with JDBC?
- Comparing two Pandas series and printing the the difference
- How to add two pandas Series objects by handling None values?
- How does the pandas series.equals() method handle the null values?
- How to find the dot product of two pandas series objects?
- How to use the series.isin() method to check values in a series?
- How to replace NaN values of a series with the mean of elements using the fillna() method?
- How to compare two Pandas Series Objects by Applying Greater Than Condition using gt() Function?
- How to apply Greater Than or Equal operation on the elements of two series objects?
- How to get items from a series object using the get() method?
- How to remove a specified row From the Pandas Series Using Drop() method?
- Grouping array nested value while comparing 2 objects - JavaScript
- Comparing Strings with (possible) null values in java?
- How to plot two violin plot series on the same graph using Seaborn?
- Java program to print the fibonacci series of a given number using while loop
