Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Extracting Specific Data | Extracting Data
Advanced Techniques in pandas

book
Extracting Specific Data

Opgave

Swipe to start coding

Your task here is to practice. You need to extract specific data on cars. Follow the algorithm:

  1. Using the.between() method create the condition to extract data on cars where 'Price' is between 15 000(inclusive) and 20 000(exclusive).
  2. Using the .between() method create the condition to extract data on cars where 'Year' is between 2015(exclusive) and 2020(exclusive).
  3. Using the.isin() method create the condition to extract data on such values from 'Fuel_type' column: 'Plug-in Hybrid' and 'Hybrid' (the values are stored in the variable fuel_types).
  4. Unite three conditions using two and statements.

Løsning

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)

fuel_types = ['Plug-in Hybrid', 'Hybrid']
# Put the condition on the column 'Price'
condition_1 = data['Price'].between(15000, 20000, inclusive = 'left')
# Put the condition on the column 'Year'
condition_2 = data['Year'].between(2015, 2020, inclusive = 'neither')
# Put the condition on the column 'Fuel_type'
condition_3 = data['Fuel_type'].isin(fuel_types)

# Unite three conditions
data_extracted = data.loc[condition_1 & condition_2 & condition_3]

print(data_extracted.head())

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 4
single

single

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)

fuel_types = ['Plug-in Hybrid', 'Hybrid']
# Put the condition on the column 'Price'
condition_1 = data['___'].___(___, 20000, inclusive = '___')
# Put the condition on the column 'Year'
condition_2 = ___['Year'].___(___, ___, inclusive = '___')
# Put the condition on the column 'Fuel_type'
condition_3 = data['Fuel_type'].___

# Unite three conditions
data_extracted = data.___[condition_1 ___ condition_2 ___ condition_3]

print(data_extracted.head())

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt