Skip to content

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, it's essential to understand the growth trends in charging facilities and sales 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 is stored in two CSV files:


private_ev_charging.csv

VariableDescription
yearYear of data collection
private_portsThe number of available charging ports owned by private companies in a given year
private_station_locationsThe number of privately owned station locations for EV charging

public_ev_charging.csv

VariableDescription
yearYear of data collection
public_portsThe number of available charging ports under public ownership in a given year
public_station_locationsThe 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:

VariableDescription
VehicleElectric vehicle model
yearYear of data collection
salesThe number of vehicles sold in the US
# Import required libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Start your code here!

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 anti-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 correctly
df_temp.drop('_merge', axis=1, inplace=True)

# Get total sales grouping by each year
ev_total_sales = ev_sales.groupby('year')['sales'].sum().reset_index()

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


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')

plt.show()

solution = 'public'