The between()
method in Pandas is used to filter values within a specified range.
Example
import pandas as pd
# create a Series
data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# use between() to filter values between 3 and 7
filtered_data = data[data.between(3, 7)]
print(filtered_data)
'''
Output
2 3
3 4
4 5
5 6
6 7
dtype: int64
'''
between() Syntax
The syntax of the between()
method in Pandas is:
Series.between(left, right, inclusive='both')
between() Arguments
The between()
method takes following arguments:
left
- the lower boundary of the rangeright
- the upper boundary of the rangeinclusive
(optional) - specifies whether the boundaries are inclusive
between() Return Value
The between()
method returns a boolean Series of the same length as the input Series. This boolean Series indicates whether each element in the original Series falls within the specified range.
Example 1: Filter Values Within a Specified Range
import pandas as pd
# create Series with temperature data
temperatures = pd.Series([18, 22, 25, 30, 35, 19, 28])
# use between() to filter values between 20 and 30 degrees (inclusive)
in_range = temperatures.between(20, 30)
print("Boolean Series indicating values in range:\n", in_range)
# filter the original series
filtered_temperatures = temperatures[in_range]
print("\nFiltered Temperatures:\n", filtered_temperatures)
Output
Boolean Series indicating values in range: 0 False 1 True 2 True 3 True 4 False 5 False 6 True dtype: bool Filtered Temperatures: 1 22 2 25 3 30 6 28 dtype: int64
Here, the between()
method is used on the temperatures Series to find temperatures between 20 and 30 degrees (inclusive) which produces the boolean Series in_range.
Then, in_range is used to filter temperatures, resulting in filtered_temperatures, which contains only the values within the specified range.
Example 2: Filter Dates Using between()
import pandas as pd
# create a Series with dates
dates = pd.Series(pd.date_range(start='2023-01-01', end='2023-01-10'))
# filter dates between 2023-01-04 and 2023-01-07
filtered_dates = dates[dates.between('2023-01-04', '2023-01-07')]
print(filtered_dates)
Output
3 2023-01-04 4 2023-01-05 5 2023-01-06 6 2023-01-07 dtype: datetime64[ns]
In this example, we have created the dates Series containing dates from January 1 to January 10, 2023.
We then used between()
in this dates Series to filter out dates between January 4 and January 7, 2023, inclusive.
Example 3: Filter Numbers Excluding Boundaries
import pandas as pd
# create a Series with range of numbers
numbers = pd.Series(range(1, 11))
# filter numbers between 3 and 8, excluding boundaries
exclusive_range = numbers[numbers.between(3, 8, inclusive='neither')]
print(exclusive_range)
Output
3 4 4 5 5 6 6 7 dtype: int64
Here, the between()
method is used to select numbers between 3 and 8, but the boundaries (3 and 8) are excluded due to inclusive='neither'
.
Hence, exclusive_range contains numbers from the numbers Series that are greater than 3 and less than 8.
Example 4: Filter String Values Using between()
import pandas as pd
# create a Series with strings
fruits = pd.Series(['apple', 'banana', 'cherry', 'elderberry', 'date'])
# filter fruits between banana and date
filtered_fruits = fruits[fruits.between('banana', 'date')]
print(filtered_fruits)
Output
1 banana 2 cherry 4 date dtype: object
In the above example, the between()
method is used on the fruits Series to select fruit names that are alphabetically between banana
and date
(inclusive).