Introduction
In this tutorial, you'll learn how to analyze car sales data using Python and pandas, a powerful tool for data analysis. This tutorial is inspired by the recent surge in Chinese car sales in the UK, where sales jumped from 384 in 2015 to 285,000 in 2026. We'll walk through how to load, clean, and visualize this type of data to understand trends and patterns. By the end, you'll have a working Python script that can process similar datasets and generate meaningful insights.
Prerequisites
Before starting, you'll need to have the following installed on your computer:
- Python 3 (version 3.6 or higher)
- pip (Python package installer)
- jupyter notebook (optional but recommended for easier data exploration)
If you're new to Python, we recommend installing Anaconda, which includes Python, pip, and Jupyter Notebook in one easy-to-use package.
Step-by-Step Instructions
Step 1: Install Required Python Packages
First, we need to install the packages we'll use for data analysis. Open your terminal or command prompt and run:
pip install pandas matplotlib
This installs pandas, a powerful library for data manipulation, and matplotlib, a library for creating visualizations.
Step 2: Create a New Python File
Create a new file called chinese_car_sales.py in your preferred code editor. This file will contain our analysis script.
Step 3: Import Required Libraries
At the top of your Python file, add the following code to import the necessary libraries:
import pandas as pd
import matplotlib.pyplot as plt
We import pandas for data handling and matplotlib.pyplot for creating charts.
Step 4: Create Sample Data
Since we don't have access to real data from the article, we'll create a sample dataset that mimics the growth pattern described. Add the following code to your file:
# Sample data based on the article
sales_data = {
'Year': [2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026],
'Chinese_Car_Sales': [384, 520, 750, 1200, 2100, 3800, 6500, 12000, 18000, 25000, 22000, 285000]
}
df = pd.DataFrame(sales_data)
print(df)
This creates a DataFrame with years and corresponding sales numbers. The data shows a gradual increase followed by a dramatic jump in 2026, matching the article's description.
Step 5: Display the Data
When you run the script, you'll see the data printed in a table format. This is how pandas displays tabular data. It's important to verify that the data is correctly loaded before proceeding.
Step 6: Visualize the Sales Growth
Now, let's create a chart to visualize the growth in Chinese car sales:
# Plotting the data
plt.figure(figsize=(10, 5))
plt.plot(df['Year'], df['Chinese_Car_Sales'], marker='o')
plt.title('Chinese Car Sales in the UK (2015-2026)')
plt.xlabel('Year')
plt.ylabel('Number of Sales')
plt.grid(True)
plt.show()
This code creates a line chart showing how sales increased over time. The marker='o' adds dots to each data point, and grid(True) adds a grid to make the chart easier to read.
Step 7: Calculate Growth Rate
To better understand the surge, let's calculate the percentage growth between years:
# Calculate percentage growth
df['Growth_Rate'] = df['Chinese_Car_Sales'].pct_change() * 100
print(df)
The pct_change() function calculates the percentage change from one year to the next. Multiplying by 100 converts it to a percentage.
Step 8: Identify Key Trends
Let's add some analysis to highlight the key growth periods:
# Highlight years with highest growth
high_growth_years = df[df['Growth_Rate'] > 1000] # Greater than 1000% growth
print("Years with high growth (>1000%):")
print(high_growth_years)
This filters the data to show only years where the sales growth was exceptionally high, which matches the article's mention of the 2026 surge.
Step 9: Save the Analysis
Finally, let's save the results to a CSV file for future use:
# Save the DataFrame to a CSV file
df.to_csv('chinese_car_sales_analysis.csv', index=False)
print("Data saved to 'chinese_car_sales_analysis.csv'")
This saves the cleaned and analyzed data to a file, which can be opened in Excel or other spreadsheet programs.
Summary
In this tutorial, you've learned how to analyze car sales data using Python and pandas. You've created a sample dataset, visualized the data with a chart, calculated growth rates, and saved the results. This type of analysis is essential for understanding market trends, as demonstrated by the rapid increase in Chinese car sales in the UK. By using these tools, you can process and visualize real-world data to uncover meaningful insights, just like the automotive consulting firm that tracked these sales trends.



