CO2 emissions
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    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.

    💪 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?

    Import Relevant Libraries

    # Import relevant libraries
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set(palette='colorblind')
    [2]
    # Load the data
    cars = pd.read_csv('data/co2_emissions_canada.csv')
    
    # Get the first 10 rows
    cars.head(10)
    # Getting the data info
    cars.info()

    Observation

    • There are no missing values in the data
    • There are 7385 rows or entries
    • There are 9 features
    [4]
    # Gathering the rough summary of all categorical data
    cars.select_dtypes(include=object).describe()

    Observation

    • Most of the cars in our data were manufactured by FORD
    • The most common vehicle model in the data is F-150 FFV 4X4
    • SUV - SMALL is the most common Vehicle class in our data
    • Regular Gasoline(Fuel Type=X) is the most common fuel Type

    Note
    Seeing the most common MAKE, Model, Vehicle Class, Fuel Type makes one wonder how the most frequent class of each category vary with Co2 emission

    Checking how Make(Company that Manufactured Vehicle) vary with Co2 emission

    [5]
    # Getting the average Co2_emmision by Make
    average_co2_by_Make = cars.groupby('Make')['CO2 Emissions(g/km)'].mean().to_frame()
    average_co2_by_Make.columns = ['Mean_co2_emission']
    average_co2_by_Make.sort_values(by='Mean_co2_emission',ascending=False, inplace=True)
    average_co2_by_Make
    # Visualizing the Top 10 Make with highest co2_emission
    average_co2_by_Make[:10].plot(kind='bar')
    plt.show()
    # Visualizing the Top 10 Make with Least co2_emission
    average_co2_by_Make[32:].plot(kind='bar')
    plt.show()