Skip to content
0

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
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# 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]

๐Ÿ’ช 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?
# Inspect the dataframe

cars.info()
carss = cars.drop_duplicates(keep = 'first')

carss
# Summary statistics

cars.describe().transpose()
# Visualizing the summary statistics for further insight
plt.figure(figsize=(12,8))

sns.boxenplot(data=cars)

REPORT

- Using .info() method to inspect the dataset, everything looks fine.

- There are 9 columns and 7385 observations.

- There are no missing values and also, all columns are of the correct data types.

- Looking at the boxplot, There are some outliers in the dataset but we cant be sure if they are truly extream values or not. Further analysis has to be performed on the variables to know exactly what is going on.

# What is the median engine size in liters?

median_engine_size_l = cars['Engine Size(L)'].median()

median_engine_size_l

REPORT

The median Engine Size in liters is 3.0

# 2. What is the average fuel consumption for regular gasoline (Fuel Type = X), premium gasoline (Z),ethanol (E), and diesel (D)?

avg_cpt = cars['Fuel Type'] == 'N'

cars_df1 = cars.loc[~avg_cpt]

avg_fuel_consumption = cars_df1.pivot_table(index='Fuel Type', values='Fuel Consumption Comb (L/100 km)', aggfunc='mean').sort_values(by='Fuel Consumption Comb (L/100 km)', ascending=True).reset_index()

avg_fuel_consumption

REPORT

From the computation above, Diesel which stood at almost 9 liters has the least average fuel cosumption per 100 km, follow by regular gasoline at 10 liters per 100 km. Ethanol(85) has the highest which is about 17 liters per 100 km.

โ€Œ
โ€Œ
โ€Œ