Store Sales Analysis walkthrough
Go from a raw CSV to a monthly sales chart with an explanation of what it includes.
On this page
Define the result
Which calendar month has the most usable recorded sales? This small synthetic file is for demonstrating the workflow. Its amounts have no specified currency. Each non-duplicate row represents one store transaction.
Clean, group, and plot
Save the supplied CSV next to your script. Read source codes intact, deduplicate exact rows, and use numeric conversion before recovering amounts. Group by month and use min_count=1 so an all-missing group stays missing.
import matplotlib.pyplot as plt
import pandas as pd
raw = pd.read_csv("store_sales.csv", dtype="string", keep_default_na=False)
data = raw.drop_duplicates().copy()
date = pd.to_datetime(data["Transaction Date"], format="%Y-%m-%d", errors="coerce")
quantity = pd.to_numeric(data["Quantity"], errors="coerce")
price = pd.to_numeric(data["Price Per Unit"], errors="coerce")
recorded = pd.to_numeric(data["Total Spent"], errors="coerce")
sales = recorded.fillna(quantity * price)
monthly = sales.groupby(date.dt.to_period("M")).sum(min_count=1)
fig, ax = plt.subplots(layout="constrained")
ax.plot(monthly.index.astype(str), monthly, marker="o")
ax.set(title="Store sales by month", xlabel="Month", ylabel="Recorded sales")
print(str(monthly.idxmax()), float(monthly.max()))
print(float(sales.sum()), float(monthly.sum()), int(sales.isna().sum()))
plt.show()
Download image
2023-04 21.5 67.0 65.0 1
Report the finding and its limitations
April 2023 recorded the highest usable monthly sales, at 21.5. The monthly sum is 65 versus 67 overall because one usable amount of 2 has an unreadable date. One other transaction has an unresolved amount. The series falls in March before increasing in April.