The US Government's Alternative Fuels Data Center collects records of electric vehicle (EV) charging infrastructure, including charging ports and station locations, as well as sales of electric vehicles. With the EV market rapidly evolving, understanding trends in charging facilities and sales is essential to inform strategic planning.
As a data scientist working for a leading EV charging network operator, you recognize the potential in this data and start wrangling and visualizing the aggregated yearly data.
This yearly data captured in December of each year encompasses a record of EV charging port installations and station localities spanning roughly ten years, capturing both public and private charging environments.
The Data
private_ev_charging.csv
| Variable | Description |
|---|---|
year | Year of data collection |
private_ports | The number of available charging ports owned by private companies in a given year |
private_station_locations | The number of privately owned station locations for EV charging |
public_ev_charging.csv
| Variable | Description |
|---|---|
year | Year of data collection |
public_ports | The number of available charging ports under public ownership in a given year |
public_station_locations | The number of publicly owned station locations for EV charging |
The sales information is available for each model and year in the ev_sales.csv file:
| Variable | Description |
|---|---|
Vehicle | Electric vehicle model |
year | Year of data collection |
sales | The number of vehicles sold in the US |
# Import required libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns# Reading the CSV files
ev_sales = pd.read_csv("ev_sales.csv")
priv_ev_charging = pd.read_csv("private_ev_charging.csv")
pub_ev_charging = pd.read_csv("public_ev_charging.csv")
# Filtring for 2018 EV sales
ev_sold_2018 = ev_sales[ev_sales["year"] == 2018]
#Suming the EV sales for 2018
ev_sales_2018 = ev_sold_2018["sales"].sum().astype(int)
print(f"The total number of EV sold in 2018 is: {ev_sales_2018}")
#Ploting EV sales per year
all_ev_sold = ev_sales.groupby("year")["sales"].sum().reset_index()
sales_plot = sns.catplot(data = all_ev_sold, x = "year", y = "sales", kind = "bar")
sales_plot.fig.suptitle("EV Sales Per Year" , y = 1.05)
sales_plot.set(xlabel = "Year", ylabel = "Number of EV sold")
plt.show()
#Merging all tables
full_ev_table = ev_sales.merge(priv_ev_charging, on = "year", how = "left").merge(pub_ev_charging, on = "year", how = "left")
#Selecting sales columns without N/A
full_ev_table = full_ev_table[full_ev_table["sales"].notna()]
# Create a figure and axis object
fig, ax = plt.subplots()
# Plotting line trend
sns.lineplot(data=full_ev_table, x='year', y='private_ports', label='Private Ports', ci = None)
sns.lineplot(data=full_ev_table, x='year', y='public_ports', label='Public Ports', ci = None)
sns.lineplot(data=full_ev_table, x='year', y='sales', label='Total Sales', ci = None)
# Adding titles and labels
ax.set_title('EV Ports and Sales Over Time')
ax.set(xlabel='Year', ylabel='EV Sales Count')
# Show the legend
ax.legend(loc='upper left')
# Show the plot
plt.show()
# Did vehicle sales and number of private and public ports show the same trend?
trend = "same"