Skip to content

## Everyone Can Learn Python Scholarship

### 1️⃣ Python 🐍 - CO2 Emissions

Now let's now move on to the competition and challenge.

### 📖 Background

You volunteer for a public policy advocacy organization in Canada, and your colleague asked you to help her draft recommendations for guidelines on CO2 emissions rules.

After researching emissions data for a wide range of Canadian vehicles, she would like you to investigate which vehicles produce lower emissions.

### 💾 The data I

#### You have access to seven years of CO2 emissions data for Canadian vehicles (source):

- "Make" - The company that manufactures the vehicle.
- "Model" - The vehicle's model.
- "Vehicle Class" - Vehicle class by utility, capacity, and weight.
- "Engine Size(L)" - The engine's displacement in liters.
- "Cylinders" - The number of cylinders.
- "Transmission" - The transmission type: A = Automatic, AM = Automatic Manual, AS = Automatic with select shift, AV = Continuously variable, M = Manual, 3 - 10 = the number of gears.
- "Fuel Type" - The fuel type: X = Regular gasoline, Z = Premium gasoline, D = Diesel, E = Ethanol (E85), N = natural gas.
- "Fuel Consumption Comb (L/100 km)" - Combined city/highway (55%/45%) fuel consumption in liters per 100 km (L/100 km).
- "CO2 Emissions(g/km)" - The tailpipe carbon dioxide emissions in grams per kilometer for combined city and highway driving.

The data comes from the Government of Canada's open data website.

```
# Import the pandas and numpy packages
import pandas as pd
import numpy as np
# Load the data
cars = pd.read_csv('data/co2_emissions_canada.csv')
# create numpy arrays
cars_makes = cars['Make'].to_numpy()
cars_models = cars['Model'].to_numpy()
cars_classes = cars['Vehicle Class'].to_numpy()
cars_engine_sizes = cars['Engine Size(L)'].to_numpy()
cars_cylinders = cars['Cylinders'].to_numpy()
cars_transmissions = cars['Transmission'].to_numpy()
cars_fuel_types = cars['Fuel Type'].to_numpy()
cars_fuel_consumption = cars['Fuel Consumption Comb (L/100 km)'].to_numpy()
cars_co2_emissions = cars['CO2 Emissions(g/km)'].to_numpy()
# Preview the dataframe
cars
```

```
# Look at the first ten items in the CO2 emissions array
cars_co2_emissions[:10]
```

Median engine size

```
#The median engine size in litres
median_engine_size = np.median(cars_engine_sizes)
print(median_engine_size)
```

Average fuel consumption

```
#The average car fuel consumption
mean_regular_gasoline = np.mean(cars_fuel_consumption[cars_fuel_types == 'X'])
print(mean_regular_gasoline)
mean_premium_gasoline = np.mean(cars_fuel_consumption[cars_fuel_types == 'Z'])
print(mean_premium_gasoline)
mean_ethanol = np.mean(cars_fuel_consumption[cars_fuel_types == 'E'])
print(mean_ethanol)
mean_diesel = np.mean(cars_fuel_consumption[cars_fuel_types == 'D'])
print(mean_diesel)
```

Correlation between fuel consumption and CO2 emissions.

```
#The correlation between fuel consumption and CO2 emissions
np.corrcoef(cars_fuel_consumption, cars_co2_emissions)
```

SUV-SMALL has a lower CO2 mission than MID-SIZE

```
suv_small_mean = np.mean(cars_co2_emissions[cars_classes == 'SUV - SMALL'])
print(suv_small_mean)
mid_size_mean = np.mean(cars_co2_emissions[cars_classes == 'MID-SIZE'])
print(mid_size_mean)
#Mid-size vehicle class has a lower average CO2 emission
```

Average CO2 emission

```
#Average CO2 emission for all vehicles
np.mean(cars_co2_emissions)
```