Pandas: How to Select Rows Between Two Values


You can use the following basic syntax to select rows in a pandas DataFrame where a column is between two specific values:

df_filtered = df[df['points'].between(25, 35)]

This particular example selects all rows where the value in the points column is between 25 and 35.

If you would instead like to select rows where the value in the points column is not between 25 and 35, you can add a tilde (~) before the column name:

df_filtered = df[~df['points'].between(25, 35)]

The following examples show how to use each method in practice.

Example: Select Rows Between Two Values in Pandas

Suppose we have the following pandas DataFrame that contains information about points scored by basketball players on various teams:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Mavs', 'Nets', 'Nets', 'Heat', 'Heat', 'Kings'],
                   'points': [22, 28, 35, 34, 29, 28, 23]})

#view DataFrame
print(df)

    team  points
0   Mavs      22
1   Mavs      28
2   Nets      35
3   Nets      34
4   Heat      29
5   Heat      28
6  Kings      23

We can use the following syntax to select only the rows in the DataFrame where the value in the points column is between 25 and 35:

#select rows where value in points column is between 25 and 35
df_filtered = df[df['points'].between(25, 35)]

#view filtered DataFrame
print(df_filtered)

   team  points
1  Mavs      28
2  Nets      35
3  Nets      34
4  Heat      29
5  Heat      28

Notice that only the rows where the value in the points column is between 25 and 35 have been selected.

Note that the between() function includes the values in the lower and upper bounds.

For example, the player with a points value of 35 has been included in the filtered DataFrame.

If you would instead like to only select rows where the value in the points column is not between 25 and 35, we can add a tilde (~) before the column name:

#select rows where value in points column is not between 25 and 35
df_filtered = df[~df['points'].between(25, 35)]

#view filtered DataFrame
print(df_filtered)

    team  points
0   Mavs      22
6  Kings      23

Notice that only the rows where the value in the points column is not between 25 and 35 have been selected.

Note: You can find the complete documentation for the pandas between() function here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Pandas: Select Rows where Two Columns Are Equal
Pandas: Select Rows from DataFrame Using Boolean Series
Pandas: Select Rows with NaN Values

Leave a Reply

Your email address will not be published. Required fields are marked *