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
💪 Challenge I
Help your colleague gain insights on the type of vehicles that have lower CO2 emissions. Include:
- What is the median engine size in liters?
- What is the average fuel consumption for regular gasoline (Fuel Type = X), premium gasoline (Z), ethanol (E), and diesel (D)?
- What is the correlation between fuel consumption and CO2 emissions?
- Which vehicle class has lower average CO2 emissions, 'SUV - SMALL' or 'MID-SIZE'?
- What are the average CO2 emissions for all vehicles? For vehicles with an engine size of 2.0 liters or smaller?
- Any other insights you found during your analysis?
#What is the median engine size in liters?
Engine_median = cars["Engine Size(L)"].median()
print("The median engine size in liters is: {}".format(Engine_median))
#What is the average fuel consumption for regular gasoline (Fuel Type = X), premium gasoline (Z), ethanol (E), and diesel (D)?
regular = cars[cars["Fuel Type"]== "X"]
premium = cars[cars["Fuel Type"] == "Z"]
ethanol = cars[cars["Fuel Type"] == "E"]
diesel = cars[cars["Fuel Type"] == "D"]
#Calculating the average fuel consumption
regular = regular['Fuel Consumption Comb (L/100 km)'].mean().round(3)
premium = premium['Fuel Consumption Comb (L/100 km)'].mean().round(3)
ethanol = ethanol['Fuel Consumption Comb (L/100 km)'].mean().round(3)
diesel = diesel['Fuel Consumption Comb (L/100 km)'].mean().round(3)
print("The average fuel consumption for regular gasoline is: {}\nfor premium gasoline is: {}\nfor ethanol is: {}\nand for diesel is: {}\n".format(regular, premium, ethanol, diesel))
#What is the correlation between fuel consumption and CO2 emissions?
#corr = np.corrcoef(cars_fuel_consumption[:], cars_co2_emissions[:])
#print("Correlation between fuel consumption and CO2 emissions: " + str(corr))
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(6,4))
sns.heatmap(cars[['Fuel Consumption Comb (L/100 km)', 'CO2 Emissions(g/km)']].corr(), annot=True)
plt.title('Correlation between fuel consumption and CO2 emissions')
plt.show()
#Which vehicle class has lower average CO2 emissions, 'SUV - SMALL' or 'MID-SIZE'?
SUV_SMALL = cars[cars['Vehicle Class'] == "SUV - SMALL"]
MID_SIZE = cars[cars['Vehicle Class'] == "MID-SIZE"]
SUV_SMALL_EMISSIONS = SUV_SMALL['CO2 Emissions(g/km)'].mean().round(3)
MID_SIZE_EMISSIONS = MID_SIZE['CO2 Emissions(g/km)'].mean().round(3)
print("The average CO2 emissions of vehicle class SUV - SMALL is: {}\nThe average CO2 emissions of vehicle class MID-SIZE is: {}\n\nThis means that the vehicle class that has lower average CO2 emissions is MID-SIZE".format(SUV_SMALL_EMISSIONS, MID_SIZE_EMISSIONS))
#What are the average CO2 emissions for all vehicles? For vehicles with an engine size of 2.0 liters or smaller?
ALL_VEHICLES_EMISSIONS = cars['CO2 Emissions(g/km)'].mean().round(3)
SMALLER_ENGINE_SIZE = cars[cars["Engine Size(L)"] <= 2.0]
SMALLER_ENGINE_SIZE = SMALLER_ENGINE_SIZE['CO2 Emissions(g/km)'].mean().round(3)
print("The average C02 emissions for all vehicles is: {}\nThe average CO2 emissions for an engine size of 2.0 liter or smaller is: {}".format(ALL_VEHICLES_EMISSIONS, SMALLER_ENGINE_SIZE))
INSIGHTS
It was found that there's strong correlation between fuel consumption and co2 emissions, hence cars which have a higher rate of fuel consumption tend to emit higher co2 emissions. See the heatmap above.
Considering what was previously said, the engine size plays a role in fuel consumption. The larger the engine size, the more fuel a vehicle consumes. It was found that the median enginze size was 3.0, hence we can say that 50% of the cars hava a smaller engine size therefore a lower fuel consumption.
Other factors were found to have an influence on fuel consumption. Depending on the fuel type, cars can have higer or lower fuel consumption. Based on our data analysis, it was found that the average fuel consumption for regular gasoline was 10.085, for premium gasoline 11.423, for ethanol 16.861 and for diesel 8.835. Therefore, using a certain fuel type such as diesel can help lower the co2 emissions becuase it lowers the fuel consumption. See the plot below.
Fuel type and Fuel Consumption
Vehicles with lower co2 emissions
Vehicles with lower co2 consumption
It was found that the vehicles with lower co2 emissions were:
- PICKUP TRUCK - SMALL
- MINICOMPACT
- SPECIAL PURPOSE VEHICLE
- STATION WAGON - SMALL
- STATION WAGON - MID-SIZE
- VAN - PASSENGER
- MINIVAN
- VAN - CARGO
This is consistent with our previous analysis. When we observe which vehicles have a smaller engine size and lower fuel consumption, we can see that those vehicles have smaller engine sizes and lower fuel consumption. See the plots below.