Project: Chart the Findings
The project ends with a chart and a saved file. The chart shows the findings, and the file keeps them.
Money by region
Revenue by region is the chart. The totals print first, then the bars draw.
import io
import matplotlib.pyplot as plt
import os
import pandas as pd
SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
sales["revenue"] = sales["units"] * sales["price"]
by_region = sales.groupby("region")["revenue"].sum()
print(by_region)
by_region.plot(kind="bar")
plt.title("Revenue by region")
plt.ylabel("Revenue")
plt.savefig("chart.png")
print(os.path.getsize("chart.png"))
Save the table
to_csv writes the table to a file. Reading the file back shows the saved text.
import io
import os
import pandas as pd
SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
sales["revenue"] = sales["units"] * sales["price"]
by_region = sales.groupby("region")["revenue"].sum()
by_region.to_csv("revenue_by_region.csv")
print(os.path.getsize("revenue_by_region.csv"))
print(open("revenue_by_region.csv").read())
Save the full table
index=False leaves the row numbers out of the file. The file holds the twelve orders only.
import io
import os
import pandas as pd
SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
sales.to_csv("sales_clean.csv", index=False)
print(os.path.getsize("sales_clean.csv"))
Example
import io
import matplotlib.pyplot as plt
import os
import pandas as pd
SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
sales["revenue"] = sales["units"] * sales["price"]
by_category = sales.groupby("category")["revenue"].sum().sort_values(ascending=False)
print(by_category)
by_category.plot(kind="bar")
plt.title("Revenue by category")
plt.xlabel("Category")
plt.ylabel("Revenue")
plt.savefig("chart.png")
by_category.to_csv("revenue_by_category.csv")
print(os.path.getsize("chart.png"))
print(os.path.getsize("revenue_by_category.csv"))
Key points
- Use groupby to build the numbers for the chart.
- Use df.plot to draw the chart from those numbers.
- Use plt.title and the side labels to name the chart.
- Use savefig to write the chart to a file.
- Use to_csv to save a table to a file.
In this example, the sales table loads from its CSV text. Revenue is units times price for each order. groupby adds the money up by category, and the totals print first. plot draws one bar per category with a title and side labels. savefig writes the chart, and to_csv saves the totals. getsize prints both file sizes as proof.
Note
to_csv writes the index as a first column unless index=False is given.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which call saves a frame called df to a file named out.csv?