How to use Series.between() funtion in pandas
Overview
The Series.between() function is used to obtain the boolean Series containing True for the elements that fall between the boundaries provided and False otherwise.
Syntax
Series.between(self, left, right, inclusive=True)
Parameters
-
left: This represents left boundaries. It is mandatory -
right: This represents the right boundaries. It is mandatory -
inclusive: This parameter is used to include the boundaries. it could beboth,neither,left,rightboundaries. This parameter is mandatory.
Example
import pandas as pdimport numpy as np# create Seriesnum_series = pd.Series([np.nan, 1, 0, 9, 7, 4,np.nan, 2])# using Series.between()print(num_series.between(1,3))
Explanation
-
Line 1–2: We import the libraries,
pandasandnumpy. -
Line 4: We create
num_seriesof length8. -
Lines 7: We return a boolean Series representing elements between left (
1) and right(3) using thebetween()function.
Note: The left (
1) and right(3) are inclusive.