Which hour is the busiest?
The manager asks: "Which hour is the busiest? Who books the most?"
The club has 300 bookings in a file. Counting by hand takes hours. A short program does it in a second.
In the project you may be given a data file and asked questions like these.
Key words
| Word | What it means | Example |
|---|---|---|
| CSV | A text file where each line is a record and commas separate the fields. | Sam,1,10 |
| Dataset | A collection of data about one subject. | all the bookings |
| Data frame | A table of data inside pandas, with rows and columns. | one row per booking |
| Library | Code somebody else wrote that you can use. | pandas |
| Trend | A pattern that goes up or down over time. | more bookings each week |
| Average | The total divided by how many values there are. | (12 + 8 + 10) divided by 3 is 10 |
| Axis | A line of a chart with a scale and a label. | hour along the bottom |
| Legend | A small box that says what each colour or line means. | blue is Court 1 |
The cells students fill in are left blank in this preview.
Try it: count with plain Python
text = "member,court,hour\nSam,1,10\nSam,2,11\nAisha,1,10\nLee,2,10\n"
lines = text.strip().split("\n")
counts = {}
for i in range(1, len(lines)): # start at 1 to skip the header
hour = lines[i].split(",")[2]
if hour in counts:
counts[hour] = counts[hour] + 1
else:
counts[hour] = 1
print(counts)
# Hour 10 has 3 bookings.
# Add another line to the text and run it again.
Worked example: an average
| Step | What you do | Result |
|---|---|---|
| 1 | Bookings on Monday, Tuesday and Wednesday | 12, 8 and 10 |
| 2 | Add them up | 12 + 8 + 10 = 30 |
| 3 | Count how many days | 3 |
| 4 | Divide the total by the count | 30 divided by 3 = 10 |
| 5 | Say it in words | The club has 10 bookings a day on average |
The cells students fill in are left blank in this preview.
The same job with pandas
import pandas as pd
# bookings.csv has the columns: member, court, hour, price, week
data = pd.read_csv("bookings.csv")
print(data["hour"].value_counts()) # count each hour
print(data.groupby("member").size()) # bookings per member
court_one = data[data["court"] == 1] # keep only the rows for court 1
print(len(court_one)) # bookings on court 1
print(data["price"].sum()) # the total money
print(data["price"].mean()) # the average price
# A trend over time: bookings in each week, in week order.
print(data.groupby("week").size()) # a rise or fall from week to week is a trend
Where pandas runs
data-callouts.note.label
The pandas and matplotlib blocks are for reading. The lesson window has only plain Python. Run them in the Python set-up your lecturer gives you. The tasks in this lesson use plain Python so they run anywhere.
A chart with matplotlib: bars and a legend
import matplotlib.pyplot as plt
court_one = [4, 9, 6, 3] # bookings at 9, 10, 11 and 12
court_two = [2, 5, 4, 1]
left = [8.8, 9.8, 10.8, 11.8] # Court 1 bars a little left of each hour
right = [9.2, 10.2, 11.2, 12.2] # Court 2 bars a little right
plt.bar(left, court_one, width=0.4, label="Court 1")
plt.bar(right, court_two, width=0.4, label="Court 2")
plt.title("Bookings in each hour")
plt.xlabel("Hour of the day")
plt.ylabel("Number of bookings")
plt.legend()
plt.show()
A good chart has...
| Part | Why |
|---|---|
| A title | Says what the chart is about |
| Labels on both axes | Says what the numbers mean, with units |
| The right kind of chart | Bars to compare, lines for change over time |
| A legend if there is more than one line or colour | Says what each one means |
| Clear colours | Anyone can read it, including people who cannot tell some colours apart |
The cells students fill in are left blank in this preview.
Quick check: charts and data
Have a go
Quick check: charts and data
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.