Between Function | Extract Data

# Between 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`.

# 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 = '
')]

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Секція 3. Розділ 3

Зміст курсу

# Between 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`.

# 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 = '
')]

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Секція 3. Розділ 3