CPSC 203, 2026 W1
October 1, 2026
What have we covered so far in CPSC 203?
dataframes and the pandas librarydatetime libraryWe often encounter data in tabular form.
Consider the Billboard Hot 100.
The data from the week of January 31, 2026:
| date | title | artist | rank | peakPos | lastPos | isNew | weeks |
|---|---|---|---|---|---|---|---|
| 2026-01-31 | I Just Might | Bruno Mars | 1 | 1 | 1 | False | 2 |
| 2026-01-31 | Man I Need | Olivia Dean | 2 | 2 | 4 | False | 22 |
| 2026-01-31 | The Fate Of Ophelia | Taylor Swift | 3 | 1 | 2 | False | 16 |
| 2026-01-31 | Golden | HUNTR/X: EJAE, Audrey Nuna & REI AMI | 4 | 1 | 3 | False | 31 |
| 2026-01-31 | Ordinary | Alex Warren | 5 | 1 | 5 | False | 49 |
Acronym for comma separated values
.csv for files with this formatting.date,title,artist,rank,peakPos,lastPos,isNew,weeks
2026-01-31,I Just Might,Bruno Mars,1,1,1,False,2
2026-01-31,Man I Need,Olivia Dean,2,2,4,False,22
2026-01-31,The Fate Of Ophelia,Taylor Swift,3,1,2,False,16
2026-01-31,Golden,"HUNTR/X: EJAE, Audrey Nuna & REI AMI",4,1,3,False,31
2026-01-31,Ordinary,Alex Warren,5,1,5,False,49Typical CPSC 103 workflow for CSV files:
Create a compound for each row of the Hot 100:
Create a list[Song] to store the table.
Answering a query about the data in the table requires loop(s).
For example: What is the name of the artist of the song which has been on the chart for the most weeks?
A DataFrame is a 2D table: rows = records, columns = attributes.
DataFrames have built-in operations for filtering, aggregating, and more.
.csv files for PandasIf our data is in a .csv file:
.csv files must have attribute names in row 1, and data beginning in row 2.
| Task | With loops | With DataFrames |
|---|---|---|
| Count new songs | 5+ lines | df[df['isNew']].shape[0] |
| Average peak | 4 lines | df['peakPos'].mean() |
| Filter long-running | 4 lines | df[df['weeks'] > 10] |
| Find biggest mover | 8+ lines | df.loc[df['gradient'].abs().idxmax()] |

df[df[col] > val] — filter by conditiondf.nlargest(n, col) — top n by columndf['new_col'] = expression
Adds a column to the DataFrame containing the computed value for every row.
df[df['new_col'] > val]
Filter rows by the new column.
Goal: Count songs by a specific artist.
Strategy:
Goal: Count songs that peaked at #1.
Strategy:
peakPos == 1len() or .shape[0]Goal: Find the average weeks on chart.
Strategy:
df['weeks'].mean()Goal: Average weeks, but only for top 10 songs.
Strategy:
df[df['rank'] <= 10]['weeks'].mean()Goal: Find the song with the most weeks on chart.
Strategy:
idxmax() returns the row index of the maximumdf.loc[idx] retrieves that rowGoal: Which song moved up the most?
Problem: New songs have lastPos = 0 (they weren’t on the chart last week).
Strategy:
lastPos - rankidxmax()The lastPos = 0 problem
New songs weren’t on the chart last week, so lastPos = 0.
If we compute change = lastPos - rank:
0 - 23 = -23Solution: Filter them out first.
Open CA5.1 Billboard, and load STUDENT_nb.py.
Compared to the CPSC 103 process, Pandas:
datetime: a Python library that simplifies date computation.
Objects we need
date — a calendar day (year-month-day)timedelta — a number of days you can add/subtractCore calls
today=date.today(): today’s datetoday.weekday(): an integer 0..6 with Mon=0, … , Sun=6today - timedelta(days=k): the date k days earlierLet \(w =\) today.weekday() \(\in \{0,\dots,6\}\) and note Saturday \(= 5\).
offset: How many days to step back to reach Saturday.
\[ \texttt{offset} = (w - 5)\bmod 7 \]
\[ \texttt{lastSaturday} = \texttt{today} - \texttt{timedelta(days=offset)}. \]
https://pymotw.com/2/datetime/
https://www.dataschool.io/best-python-pandas-resources/
https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf
https://queirozf.com/entries/pandas-dataframe-plot-examples-with-matplotlib-pyplot