Programming, problem solving, and algorithms

CPSC 203, 2026 W1

October 6, 2026

Announcements

  • Practice examlet 2 is available.
  • Examlet 2 next week (Thursday Oct 15 to Monday October 19).
  • Lab 4 and POTW 4 due Sunday October 11, 8pm.
  • Project 1 due Monday October 26, 8pm.

Part 1: Song History

The Question

Given a year of charts,

  • What do the paths of the number ones look like?

  • Do most songs debut at the top? How fast do they fall?

We can quantify these things, and analyze them, but a picture will help us see if there’s anything interesting in the data.

First: Sketch Where We Are Going

Draw a loose sketch of how you might visualize the paths through the Hot 100 chart for all songs which hit #1.

  • What are some reasonable axes?
  • Imagine what the path of one song looks like.
  • How many paths are there for all songs?

Key Steps

  1. Assemble the data we want to analyze.
  2. Find the list of Number 1 songs.
  3. For each of those songs, make a list of their ranks over time.
  4. Rearrange the data into a form suitable for plotting.
  5. Generate the plot.

Step 3: Filter for Multiple Possible Values

We have a list of song titles that hit #1.

Now we want all rows for those songs — their complete chart history.

Useful Pandas: .isin()

.isin(list) method returns True for rows where the value appears in the list.

We’ve already loaded some data behind the scenes…

Step 4: Reshaping for Plotting

Our data looks like this:

  • One row per song per week.
  • Multiple rows for each week.
  • Multiple rows for each song.
date title rank
2025-01-04 Song A 1
2025-01-04 Song B 15
2025-01-11 Song A 3
2025-01-11 Song B 1

But for plotting we want

  • One row per week (x-axis value).
  • One column per song (one line).
  • Rank is no longer a column.
date Song A Song B
2025-01-04 1 15
2025-01-11 3 1

Useful Pandas

  • groupby() method organizes rows into groups. In this case, one group per (date, title) pair.
    • Result is a Series with a multi-level index (date, title).
    • Each group has exactly one row, so sum() method just extracts that row’s value.
  • unstack() method takes the inner index level (title) and makes it into columns in a DataFrame.
    • Now each column is a song, each row is a date!

Try It Yourself

Basically the same steps as above, but gives a better idea of what kind of code we would expect you to be able to read vs write from scratch.

Visualization Discussion

  • What patterns do you see in the #1 songs?
  • Do most songs debut at the top?
  • How quickly do they typically fall?
  • Any songs with unusual trajectories?

Part 2: Staying Power

The Question

Does a strong debut mean a song will stick around longer?

Or do slow climbers have more staying power?

Let’s investigate with a scatter plot!

Key Steps

  1. Assemble the data we want to analyze.
  2. Find the debut position of each song.
  3. Find the total number of weeks each song stayed on the chart.
  4. Rearrange the data into a form suitable for plotting.
  5. Generate the plot.

Useful Pandas

  • rename() method allows you to rename columns.
  • toframe() method can convert a Series into a DataFrame.
  • pd.merge() function can combine the data in two tables.

Visualization Discussion

  • Is there a correlation between debut position and staying power?
  • Any outliers — songs that debuted low but stayed forever?
  • Or songs that debuted at #1 but disappeared quickly?

Practice for yourself:

Basically the same steps as above, but gives a better idea of what kind of code we would expect you to be able to read vs write from scratch.

Summary

Concept Code
Filter by condition df[df['rank'] == 1]
Get unique values df['title'].unique()
Filter by list df[df['title'].isin(list)]
Reshape for plotting .groupby([...]).unstack()
Combining tables pd.merge()

Resources

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