Maciej Gubała/

Competition - Everyone Can Learn Python Scholarship


Monty Python Canada adventures and tricycle story: can Python be quicker than Canadian car ?

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.

Hi Susan,

Thank you for assigning this task. I have to say that the topic is both interesting and timely, as it touches on several important issues such as car brands, energy and fuel, and environmental sustainability.

In the first five chapters, I have addressed the questions you posed. Chapter 6 delves further into the topic by exploring vehicles that produce lower emissions and drafting recommendations for CO2 emissions regulations based on this research.

Overall, since the topic is python scholarship I tried to find answer for following questions:
  • can Python be quicker than a Canadian car ?

💾 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
import matplotlib.pyplot as plt
import statsmodels
import as px
import plotly.graph_objects as go 
from plotly.subplots import make_subplots

# 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()
There are 1314 duplicated entries in our dataset, that should be removed

💪 Challenge I

Help your colleague gain insights on the type of vehicles that have lower CO2 emissions. Include:

  1. What is the median engine size in liters?
  2. What is the average fuel consumption for regular gasoline (Fuel Type = X), premium gasoline (Z), ethanol (E), and diesel (D)?
  3. What is the correlation between fuel consumption and CO2 emissions?
  4. Which vehicle class has lower average CO2 emissions, 'SUV - SMALL' or 'MID-SIZE'?
  5. What are the average CO2 emissions for all vehicles? For vehicles with an engine size of 2.0 liters or smaller?
  6. Any other insights you found during your analysis?

1. What is the median engine size in liters?

y= cars["Engine Size(L)"]
fig = go.Figure()
fig.update_layout(title="Median Engines Size", yaxis_title="Median value")
fig.layout.xaxis2 = go.layout.XAxis(overlaying='x', range=[0, 2], showticklabels=False)
fig.add_scatter(x = [0, 2], y = [3, 3], mode='lines', xaxis='x2',
                            showlegend=False, line=dict(dash='dash', color = "#FA0087", width = 2))
fig.add_trace(go.Box(y=y, name='Engines size', marker_color = '#482878'))

ad1. Median Engine Size is 3 L.

  • AI Chat
  • Code