Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Between Function | Extract Data
Advanced Techniques in pandas

Between FunctionBetween Function

Let's examine our dataset a little bit. We have numerical columns, for instance 'Engine_volume'. Imagine you want information about all cars with an 'Engine_volume' less than 3, but greater than 2. Using the .loc[]statement, we can easily do this.

However, knowing that Python provides a special function that can extract data between two values without using two conditions will be useful. This function is titled .between(left_bound, right_bound). You can apply it to numerical columns specifying the numbers' left and right bounds. Look at the example and learn how we can combine .between() and .loc[] statements.

The code below extracts data where 'Engine_volume' >= 2 and 'Engine_volume' <= 3, but what should we do to make one or even two boundaries exclusive? Let's find out using the same example. You can add an additional argument to the .between() function.

  • .between(2, 3, inclusive = 'right') - extracts data where 'Engine_volume' > 2 and 'Engine_volume' <= 3;
  • .between(2, 3, inclusive = 'left') - extracts data where 'Engine_volume' >= 2 and 'Engine_volume' < 3;
  • .between(2, 3, inclusive = 'both') - extracts data where 'Engine_volume' >= 2 and 'Engine_volume' <= 3. The result will be the same as without using inclusive = 'both';
  • .between(2, 3, inclusive = 'neither') - extracts data where 'Engine_volume' > 2 and 'Engine_volume' < 3.
question-icon

Your task here is to extract data corresponding to the comments.

# Extract data where values from the column 'Year' are greater than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

Sección 3. Capítulo 3
course content

Contenido del Curso

Advanced Techniques in pandas

Between FunctionBetween Function

Let's examine our dataset a little bit. We have numerical columns, for instance 'Engine_volume'. Imagine you want information about all cars with an 'Engine_volume' less than 3, but greater than 2. Using the .loc[]statement, we can easily do this.

However, knowing that Python provides a special function that can extract data between two values without using two conditions will be useful. This function is titled .between(left_bound, right_bound). You can apply it to numerical columns specifying the numbers' left and right bounds. Look at the example and learn how we can combine .between() and .loc[] statements.

The code below extracts data where 'Engine_volume' >= 2 and 'Engine_volume' <= 3, but what should we do to make one or even two boundaries exclusive? Let's find out using the same example. You can add an additional argument to the .between() function.

  • .between(2, 3, inclusive = 'right') - extracts data where 'Engine_volume' > 2 and 'Engine_volume' <= 3;
  • .between(2, 3, inclusive = 'left') - extracts data where 'Engine_volume' >= 2 and 'Engine_volume' < 3;
  • .between(2, 3, inclusive = 'both') - extracts data where 'Engine_volume' >= 2 and 'Engine_volume' <= 3. The result will be the same as without using inclusive = 'both';
  • .between(2, 3, inclusive = 'neither') - extracts data where 'Engine_volume' > 2 and 'Engine_volume' < 3.
question-icon

Your task here is to extract data corresponding to the comments.

# Extract data where values from the column 'Year' are greater than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater or equal than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

# Extract data where values from the column 'Year' are greater than 2010 and less or equal than 2015
data.loc[data['Year'].between(2010, 2015, inclusive = '
')]

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

Sección 3. Capítulo 3
some-alt