0
0
PandasHow-ToBeginner · 3 min read

How to Use between() in pandas for Filtering Data

In pandas, use the between() method to filter data by checking if values lie between two boundaries. It returns a boolean Series that you can use to select rows where values are within the specified range, inclusive by default.
📐

Syntax

The between() method is used on a pandas Series or DataFrame column to check if values fall between two limits.

  • left: The lower boundary of the range.
  • right: The upper boundary of the range.
  • inclusive: Whether to include the boundaries. It can be 'both' (default), 'left', 'right', or 'neither'.
python
Series.between(left, right, inclusive='both')
💻

Example

This example shows how to filter a DataFrame to keep rows where the 'age' column values are between 20 and 30, inclusive.

python
import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
        'age': [25, 17, 30, 35, 22]}
df = pd.DataFrame(data)

# Filter rows where age is between 20 and 30 inclusive
filtered_df = df[df['age'].between(20, 30)]
print(filtered_df)
Output
name age 0 Alice 25 2 Charlie 30 4 Eva 22
⚠️

Common Pitfalls

One common mistake is forgetting that between() includes the boundary values by default. If you want to exclude boundaries, you must set inclusive='neither'.

Another pitfall is using between() on a DataFrame directly instead of a Series or column, which will cause an error.

python
import pandas as pd

data = {'score': [50, 75, 85, 90, 100]}
df = pd.DataFrame(data)

# Wrong: applying between on DataFrame causes error
# filtered_wrong = df.between(70, 90)  # This will raise AttributeError

# Correct: apply between on a column (Series)
filtered_correct = df[df['score'].between(70, 90, inclusive='left')]
print(filtered_correct)
Output
score 1 75 2 85 3 90
📊

Quick Reference

ParameterDescriptionDefault
leftLower bound of the rangeRequired
rightUpper bound of the rangeRequired
inclusiveInclude boundaries: 'both', 'left', 'right', 'neither''both'

Key Takeaways

Use between() on a pandas Series to filter values within a range easily.
By default, between() includes both boundary values; change inclusive to adjust this.
Always apply between() on a Series or DataFrame column, not on the entire DataFrame.
The method returns a boolean Series useful for filtering rows in a DataFrame.
You can customize boundary inclusion with the inclusive parameter for precise filtering.