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

`cars.head()`

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

**1. What is the median engine size in liters?**

```
eng_l_median = np.median(cars_engine_sizes)
print('Median Engine Size in Liters= ', eng_l_median)
```

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

```
fuel_consump_type = cars.groupby('Fuel Type')['Fuel Consumption Comb (L/100 km)'].mean()
print('Average Fuel Consumption for Regular Gasoline:', '\n', fuel_consump_type.loc[['X', 'Z', 'E', 'D']])
```

**3. What is the correlation between fuel consumption and CO2 emissions?**

```
import matplotlib.pyplot as plt
x_fuel = cars['Fuel Consumption Comb (L/100 km)']
y_co2 = cars['CO2 Emissions(g/km)']
# plot
fig, ax = plt.subplots()
ax.scatter(x_fuel, y_co2, linewidth=2.0)
ax.set(xlim=(0, 30), xticks=np.arange(0, 30, 10),
ylim=(0, 600), yticks=np.arange(0, 600, 100))
plt.show()
```

From above figure, the relation is linear, where the fuel consumption increase the CO2 emmissions increase

**4. Which vehicle class has lower average CO2 emissions, 'SUV - SMALL' or 'MID-SIZE'?**

```
cars_type = pd.DataFrame({'cars_classes':cars_classes, 'cars_co2_emissions':cars_co2_emissions})
cars_spec_type = cars_type.query("cars_classes in ('SUV - SMALL', 'MID-SIZE')")
```

```
import seaborn as sns
fig, ax = plt.subplots()
sns.boxplot(data=cars_spec_type, x="cars_classes", y="cars_co2_emissions")
plt.show()
```

```
cars_avg_type = cars_type.groupby('cars_classes')['cars_co2_emissions'].mean()
print('Average CO2 Emmissions for Car Classes "SUV - SMALL", "MID-SIZE":', '\n', cars_avg_type.loc[['SUV - SMALL', 'MID-SIZE']])
```