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
# Load the data
sales_df = pd.read_csv('ev_sales.csv')
private_df = pd.read_csv('private_ev_charging.csv')
public_df = pd.read_csv('public_ev_charging.csv')
# 1. Total EV sales in 2018
ev_sales_2018 = sales_df[sales_df['year'] == 2018]['sales'].sum()
# 2. Plot trends
# Aggregate sales by year
sales_by_year = sales_df.groupby('year')['sales'].sum().reset_index()
# Merge all dataframes on 'year'
combined_df = pd.merge(private_df, public_df, on='year')
combined_df = pd.merge(combined_df, sales_by_year, on='year')
# Plot
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(combined_df['year'], combined_df['private_ports'], label='Private Ports', marker='o')
ax.plot(combined_df['year'], combined_df['public_ports'], label='Public Ports', marker='o')
ax.plot(combined_df['year'], combined_df['sales'], label='EV Sales', marker='o')
ax.set_title('EV Infrastructure and Sales Trends')
ax.set_xlabel('Year')
ax.set_ylabel('Count')
ax.legend()
ax.grid(True)
# 3. Trend analysis between 2015 and 2018
trend_data = combined_df[(combined_df['year'] >= 2015) & (combined_df['year'] <= 2018)]
private_trend = trend_data['private_ports'].iloc[-1] > trend_data['private_ports'].iloc[0]
public_trend = trend_data['public_ports'].iloc[-1] > trend_data['public_ports'].iloc[0]
sales_trend = trend_data['sales'].iloc[-1] > trend_data['sales'].iloc[0]
trend = 'same' if private_trend == public_trend == sales_trend else 'different'