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 snsimport pandas as pd
import matplotlib.pyplot as plt
# 1. Load datasets
private_df = pd.read_csv('private_ev_charging.csv')
public_df = pd.read_csv('public_ev_charging.csv')
sales_df = pd.read_csv('ev_sales.csv')
# 2. Calculate total EV sales for 2018
ev_sales_2018 = sales_df.loc[sales_df['year'] == 2018, 'sales'].sum()
# 3. Aggregate sales by year
sales_yearly = sales_df.groupby('year', as_index=False)['sales'].sum()
# 4. Merge datasets by year
merged_df = (
pd.merge(private_df, public_df, on='year')
.merge(sales_yearly, on='year')
)
# 5. Plot trends
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(merged_df['year'], merged_df['private_ports'], label='Private Ports', marker='o')
ax.plot(merged_df['year'], merged_df['public_ports'], label='Public Ports', marker='o')
ax.plot(merged_df['year'], merged_df['sales'], label='EV Sales', marker='o')
ax.set_xlabel('Year')
ax.set_ylabel('Count')
ax.set_title('EV Sales vs Charging Ports Over Time')
ax.legend()
plt.tight_layout()
# 6. Compare trends 2015–2018
def trend_direction(series, start_year, end_year):
start_val = series.loc[merged_df['year'] == start_year].values[0]
end_val = series.loc[merged_df['year'] == end_year].values[0]
if end_val > start_val:
return 'increasing'
elif end_val < start_val:
return 'decreasing'
else:
return 'flat'
private_trend = trend_direction(merged_df['private_ports'], 2015, 2018)
public_trend = trend_direction(merged_df['public_ports'], 2015, 2018)
sales_trend = trend_direction(merged_df['sales'], 2015, 2018)
trend = 'same' if len({private_trend, public_trend, sales_trend}) == 1 else 'different'
ev_sales_2018, trend