Contenido del Curso
Advanced Techniques in pandas
Advanced Techniques in pandas
Between 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 usinginclusive = 'both'
;.between(2, 3, inclusive = 'neither')
- extracts data where'Engine_volume' > 2 and 'Engine_volume' < 3
.
¡Gracias por tus comentarios!