Python | Pandas Series.eq ()

NumPy | Python Methods and Functions

Pandas series.eq() is used to compare each element of the Caller series with the missing series. It returns True for every element that is equal to an element in the passed series.

Note: results are returned based on a comparison of caller series = other series.

Syntax: Series.eq (other, level = None, fill_value = None)

Parameters:
other: other series to be compared with
level: int or name of level in case of multi level
fill_value: Value to be replaced instead of NaN

Return type: Boolean series

Example # 1: Handling null values ​​

In this example, two series are created using pd.Series () . The series contains several zero values ​​and several equal values ​​with the same indices.  .eq () are compared using .eq () and 5 is passed to fill_value to replace NaN values ​​with 5.

# pandas module import

import pandas as pd 

 
# import numpy module

import numpy as np 

  
# create series 1

series1 = pd.Series ([ 70 , 5 , 0 , 225 , < / code> 1 , 16 , np.nan, 10 , np.nan]) 

 
# create series 2

series2 = pd.Series ([ 70 , np.nan, 2 , 23 , 1 , 95 , 53 , 10 , 5 ]) 

 
# NaN replacement

replace_nan = 5

  
# call and return to the result variable

result = series1.eq (series2, fill_value = replace_nan )

 
# display
result 

Output:
As shown in the output, True was returned wherever the value in the caller row was equal to the value in the passed row. You can also see that the zero values ​​have been replaced with 5 and the comparison has been done using that value. 

Example # 2: Calling Series with str objects
In this example, two series are created using pd.Series (). The series also contains some string values. In the case of strings, the comparison is performed against their

# pandas module import

import pandas as pd 

 
# numpy module import

import numpy as np 

 
# create series 1

series1 = pd.Series ([ `Aaa` , 10 , `cat` , 43 , 9 , `Dog` , np.nan, `x` , np.nan]) 

 
# create episode 2

series2 = pd.Series ([ `vaa` , np.nan, ` Cat` , 23 , 5 , `Dog` , 54 , ` x` , np.nan]) 

 
# Replace NaN

replace_na n = 10

  
# call and return to the result variable

result = series1.eq (series2, fill_value = replace_nan)

 
# display
result 

Output:
As you can see from the output, in the case of strings, the comparison was made using their ASCII values. True was returned wherever the string in the Caller series was equal to the string in the passed series. 





Get Solution for free from DataCamp guru