• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Analyzing Sales Data with Python: Identify Monthly Trends

Hilton D

I have a sales dataset containing information about daily sales for a retail store. Each record includes the date and the corresponding sales amount. Here's a sample of the dataset:

sales_data = {
    'date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-02-01', '2023-02-02', '2023-02-03'],
    'sales_amount': [500, 600, 700, 550, 620, 720]

I want to analyze this data to identify monthly sales trends. Specifically, I would like to:

  1. Calculate the total sales for each month and store the results in a new data structure.
  2. Determine the month with the highest total sales.
  3. Plot a bar chart or line chart to visualize the monthly sales trends.

I've only recently started using Python for data analysis, and after trying several articles, I was unable to find the answer. Using Python and well-known data analysis packages like Pandas and Matplotlib, could someone please offer code examples or instructions on how to accomplish these tasks?
You can achieve these tasks using the Pandas and Matplotlib libraries in Python. First, make sure you have these libraries installed:

pip install pandas matplotlib

Now, you can use the following code to analyze your sales dataset:

import pandas as pd
import matplotlib.pyplot as plt

# Sample data
sales_data = {
    'date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-02-01', '2023-02-02', '2023-02-03'],
    'sales_amount': [500, 600, 700, 550, 620, 720]

# Convert the data to a Pandas DataFrame
df = pd.DataFrame(sales_data)
df['date'] = pd.to_datetime(df['date'])  # Convert 'date' column to datetime format

# Extract month and year from the 'date' column
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year

# Group by month and sum the sales_amount for each month
monthly_sales = df.groupby(['year', 'month'])['sales_amount'].sum().reset_index()

# Find the month with the highest total sales
max_sales_month = monthly_sales.loc[monthly_sales['sales_amount'].idxmax()]

# Print the monthly sales data and the month with the highest sales
print("Monthly Sales Data:")

print("\nMonth with the Highest Sales:")

# Plotting
plt.figure(figsize=(10, 6))
plt.plot(monthly_sales['date'], monthly_sales['sales_amount'], marker='o')
plt.title('Monthly Sales Trends')
plt.ylabel('Total Sales Amount')

This code converts your data into a Pandas DataFrame, extracts the month and year from the date, calculates the total sales for each month, finds the month with the highest sales, and finally, plots the monthly sales trends.

Note: This assumes that your sales_data dictionary represents a time series dataset with a 'date' column. If your dataset is more complex, you may need to adapt the code accordingly.

Anyone Can Learn to Code! 550+ Hours of Courses Content!

New Threads

Latest posts

Buy us a coffee!

Top Bottom