Python Pandas between() method – A Quick Guide!

Python Between() Method

Hello, readers! In our series of the Pandas module, we will discuss an unaddressed yet important function – Python Pandas between() function in detail.

So, let us get started!

Use of the Pandas between() method

Python Pandas module is basically used to deal with the data value residing in rows and columns i.e. in a kind of table/matrix form. Within which, we often come across data variables holding values of numeric types.

Analysis and transformation of data are necessary before processing them to any kind of action such as modeling, etc.

In simple words, the Python Pandas between() function helps us for easy analysis in terms of comparison and last moment checks.

The between() function checks for the value present between the start and the end value passed to the function.

That is, amongst a range of values, it will check which data elements fall between the start and end value passed.

Let us now try understanding the structure of the same!

Syntax – Python Pandas between() method

Have a look at the below syntax!

Series.between(start, end, inclusive=True)
  • start: This is the starting value from which the check begins.
  • end: The check halts at this value.
  • inclusive: If True, it includes the passed ‘start’ as well as ‘end’ value which checking. If set to ‘False‘, it excludes the ‘start’ and the ‘end’ value while performing the check.

To add, Python Pandas between() function works well for numeric values and 1-dimensional DataFrames only.

Let us now try to analyze the function through some examples.

1. Python between() function with inclusive set to ‘True’

In this example, we have created a 1-D Dataframe using pandas.DataFrame() function.


import pandas as pd 
data = {"Roll-num": [10,20,30,40,50,60,70], "Age":[12,21,13,20,14,13,15], "NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}

block = pd.DataFrame(data)
print("Original Data frame:\n")


Have a look at the below dataframe!

Original Data frame:

   Roll-num  Age    NAME
0        10   12    John
1        20   21  Camili
2        30   13  Rheana
3        40   20  Joseph
4        50   14  Amanti
5        60   13   Alexa
6        70   15    Siri

Now, we have applied between() method on the ‘Age’ variable of the data frame.

By setting inclusive to True, it will now include and check what all values fall between 12 and 15 (including 12 and 15) and then return true for the indexes whose Age falls between the set range.

block["Age"].between(12, 15, inclusive = True)  


As a result, it returns False for index 1 and 3 because, the values fall beyond the range 12 to 15.

0     True
1    False
2     True
3    False
4     True
5     True
6     True
Name: Age, dtype: bool

2. Python between() function with Categorical variable

Now, let us see what it yields for a string or categorical data.

If we pass a string or non-numeric variable to the Pandas between() function, it compares the start and end values with the data passed and returns True if the data values match either of the start or end value.


block["NAME"].between("John", "Joseph", inclusive = True)   


As a result, only two values are returned to be True.

0     True
1    False
2    False
3     True
4    False
5    False
6    False
Name: NAME, dtype: bool

3. Printing the values obtained from between() function

In this example, we will try to print the data that falls between 12 and 15 using Pandas between() function.


btwn = block["Age"].between(12, 15, inclusive = False)  


As we have set inclusive to False, it will check for the values that lay between 12 and 15 excluding 12 and 15 itself. So, it pushes 13, 14, and 15 as output.

     Roll-num	Age	NAME
2	30	13	Rheana
4	50	14	Amanti
5	60	13	Alexa


By this, we have come to the end of this topic. Feel free to comment below, incase you come across any question.

For more such posts related to Python, Stay tuned and till then, Happy Learning!! 馃檪