How to use the Series.le() function in pandas
The Series.le() function
The pandas Series.le() function compares two series. This is done element-wise. It returns True if the Series a is less than or equal to Series b. Otherwise, it returns False.
Syntax
Series.le(other, level=None, fill_value=None, axis=0)
Parameters
-
other: It represents the otherSeriesto be compared. -
level(optional): The default isNone. It represents the level (in the case of multilevel) to count along. -
fill_value: It represents the value to replace the missing values in theSeries. If the element in both correspondingSeriesindexes isNaN, the result of filling at that specific index will beNone. The default value isNone. -
axis: This represents the axis to perform the operation on. Value0indicates rows and1indicates columns.
Example
The following code demonstrates how to use the Series.le() function in pandas:
import pandas as pdimport numpy as np# create Seriesa = pd.Series([7, 12, 9, np.nan, 23, 7, np.nan])b = pd.Series([10, np.nan, 28, 45, 73, 40, np.nan])# value to fill nanreplace_nan = 5# finds the smaller or equal to element between the two Series using Series.le()print(a.le(b, fill_value=replace_nan))
Explanation
-
Line 1–2: We import the libraries
pandasandnumpy. -
Lines 5–7: We create two Series,
aandb. -
Line 10: We create a variable
replace_nanand assign a value to it. This replaces theNaNvalues in theSeries. -
Line 13: We find smaller or equal elements between the Series
aandbusing thele()function.
Note: The default value for
fill_valuewill be used if the element in both correspondingSeriesindexes isNaN.