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

Conteúdo do Curso

Advanced Techniques in pandas

Advanced Techniques in pandas

1. Getting Familiar With Indexing and Selecting Data
2. Dealing With Conditions
3. Extracting Data
4. Aggregating Data
5. Preprocessing Data

bookBetween Method

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 method 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() method.

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

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt