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
# Import required libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load the datasets
private_ev_charging = pd.read_csv("private_ev_charging.csv")
public_ev_charging = pd.read_csv("public_ev_charging.csv")
ev_sales = pd.read_csv("ev_sales.csv")
# Perform an outer join to keep only the rows with complete information
df_combined = private_ev_charging.merge(public_ev_charging, on='year', how='outer', indicator=True)
df_temp = df_combined[df_combined['_merge'] == 'both']
# Drop the _merge column as it's no longer needed
df_temp = df_temp.drop(columns=['_merge'])
# Get total sales grouping by each year
ev_total_sales = ev_sales.groupby('year')['sales'].sum().reset_index()
# Inspect the data and save the variable
print(ev_total_sales)
ev_sales_2018 = 361315
# Left-join with sales
df_complete = df_temp.merge(ev_total_sales, how='left', on='year')
# Drop any rows with null values
df_complete = df_complete.dropna(subset="sales")
# Create a figure and axis object
fig, ax = plt.subplots()
# Plot each line
sns.lineplot(data=df_complete, x='year', y='private_ports', label='Private Ports')
sns.lineplot(data=df_complete, x='year', y='public_ports', label='Public Ports')
sns.lineplot(data=df_complete, x='year', y='sales', label='Total Sales', linestyle=':')
# Adding titles and labels
ax.set_title('EV Ports and Sales Over Time')
ax.set(xlabel='Year', ylabel='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"