How do I select all rows between two values in a Pandas DataFrame?

In order to select all rows between two values in a Pandas DataFrame, you can use the DataFrame.loc() method. This method takes two arguments, the start row and end row, and returns a DataFrame containing all rows between the two rows specified, inclusive. For example, if you wanted to select all rows between rows 3 and 8, you could use the following command: df.loc[3:8]. This would return a DataFrame containing the rows from 3 to 8, inclusive.


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.

x