Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Combining Your Knowledge | Extracting Data
Advanced Techniques in pandas

bookCombining Your Knowledge

If you remember, several chapters ago you were provided with information on how to write several conditions simultaneously. With the .isin() statement, you can use the same rules. For instance, an example from the previous chapter could look like this:

# The initial example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
data_extracted = data.loc[data['Manufacturer'].isin(models)]
print(data_extracted.head())

# The modified example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
condition = data['Manufacturer'].isin(models)
data_extracted = data.loc[condition]
print(data_extracted.head())

The output in these two cases will be the same.

question-icon

Your task here is to make the data satisfy three conditions: cars' categories are 'Sedan', 'Jeep', 'Coupe', AND the car has a leather interior ('Leather_interior' == Yes), AND the types of gear box are 'Variator' or 'Automatic'.

import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)

categories = ['Sedan', 'Jeep', 'Coupe']
gear_box = ['Variator', 'Automatic']

condition_1 = data['Category'].

(categories)
condition_2 = data['Leather_interior']
'Yes'
condition_3 = data['Gear_box_type'].


data_extracted = data.loc[condition_1
condition_2condition_3]

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain what the `.isin()` function does in this context?

What is the difference between using the condition directly in `.loc[]` and assigning it to a variable first?

Can you show more examples of using `.isin()` with different columns?

Awesome!

Completion rate improved to 3.03

bookCombining Your Knowledge

Desliza para mostrar el menú

If you remember, several chapters ago you were provided with information on how to write several conditions simultaneously. With the .isin() statement, you can use the same rules. For instance, an example from the previous chapter could look like this:

# The initial example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
data_extracted = data.loc[data['Manufacturer'].isin(models)]
print(data_extracted.head())

# The modified example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
condition = data['Manufacturer'].isin(models)
data_extracted = data.loc[condition]
print(data_extracted.head())

The output in these two cases will be the same.

question-icon

Your task here is to make the data satisfy three conditions: cars' categories are 'Sedan', 'Jeep', 'Coupe', AND the car has a leather interior ('Leather_interior' == Yes), AND the types of gear box are 'Variator' or 'Automatic'.

import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)

categories = ['Sedan', 'Jeep', 'Coupe']
gear_box = ['Variator', 'Automatic']

condition_1 = data['Category'].

(categories)
condition_2 = data['Leather_interior']
'Yes'
condition_3 = data['Gear_box_type'].


data_extracted = data.loc[condition_1
condition_2condition_3]

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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