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:
- Using the
.between()
method create the condition to extract data on cars where'Price'
is between15 000
(inclusive) and20 000
(exclusive). - Using the
.between()
method create the condition to extract data on cars where'Year'
is between2015
(exclusive) and2020
(exclusive). - 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 variablefuel_types
). - Unite three conditions using two and statements.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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?
Tak for dine kommentarer!
Sektion 3. Kapitel 4
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat