How to handle the null values while comparing the two series objects using series.eq() method?

PandasServer Side ProgrammingProgramming

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.

raja
Updated on 07-Mar-2022 06:54:36

Advertisements