How can I select rows between two values in Pandas?

How can I select rows between two values in Pandas?

Pandas is a popular Python library used for data manipulation and analysis. It offers a wide range of functions and methods for working with tabular data, including the ability to select specific rows based on certain criteria. One common task is selecting rows between two values, which can be easily achieved using the “between” method in Pandas. This method allows you to specify a lower and upper bound, and it will return all rows that fall within that range. This feature is useful for filtering data and extracting only the relevant information for further analysis. It is a simple and efficient way to work with large datasets in Pandas.

Pandas: 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.

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

Cite this article

stats writer (2024). How can I select rows between two values in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-rows-between-two-values-in-pandas/

stats writer. "How can I select rows between two values in Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-rows-between-two-values-in-pandas/.

stats writer. "How can I select rows between two values in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-rows-between-two-values-in-pandas/.

stats writer (2024) 'How can I select rows between two values in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-rows-between-two-values-in-pandas/.

[1] stats writer, "How can I select rows between two values in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I select rows between two values in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top