Skip to content

1. Welcome!

Markdown.

The Office! What started as a British mockumentary series about office culture in 2001 has since spawned ten other variants across the world, including an Israeli version (2010-13), a Hindi version (2019-), and even a French Canadian variant (2006-2007). Of all these iterations (including the original), the American series has been the longest-running, spanning 201 episodes over nine seasons.

In this notebook, we will take a look at a dataset of The Office episodes, and try to understand how the popularity and quality of the series varied over time. To do so, we will use the following dataset: datasets/office_episodes.csv, which was downloaded from Kaggle here.

This dataset contains information on a variety of characteristics of each episode. In detail, these are:

datasets/office_episodes.csv
  • episode_number: Canonical episode number.
  • season: Season in which the episode appeared.
  • episode_title: Title of the episode.
  • description: Description of the episode.
  • ratings: Average IMDB rating.
  • votes: Number of votes.
  • viewership_mil: Number of US viewers in millions.
  • duration: Duration in number of minutes.
  • release_date: Airdate.
  • guest_stars: Guest stars in the episode (if any).
  • director: Director of the episode.
  • writers: Writers of the episode.
  • has_guests: True/False column for whether the episode contained guest stars.
  • scaled_ratings: The ratings scaled from 0 (worst-reviewed) to 1 (best-reviewed).
# Use this cell to begin your analysis, and add as many as you would like!
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

office_episodes = pd.read_csv('datasets/office_episodes.csv')
office_episodes['episode_number'] = office_episodes['episode_number'] + 1
display(office_episodes.head(20))
# Initialize figure
fig = plt.figure()


# Set up rating based colors, sizing/marker based on guest stars

colors = []
sizes = []
marker_shape = []
idx_reg = []
idx_star = []
for i, row in office_episodes.iterrows() :

    if row['scaled_ratings'] < 0.25 :
        colors.append('red')
    elif row['scaled_ratings'] >= 0.25 and row['scaled_ratings'] < 0.50 :
        colors.append('orange')
    elif row['scaled_ratings'] >= 0.50 and row['scaled_ratings'] < 0.75 :
        colors.append('lightgreen')
    else :
        colors.append('darkgreen')
    # set up sizing system based on guest stars
    guest_appearance = row['guest_stars']
    if str(guest_appearance) == 'nan' :
        idx_reg.append(i)
        sizes.append(25)
    else :
        idx_star.append(i)
        sizes.append(250)

# Viewership vs. episode number
# identify x and y axes of plot
xval_reg = office_episodes[office_episodes['guest_stars'].isna()]['episode_number']
yval_reg = office_episodes[office_episodes['guest_stars'].isna()]['viewership_mil']

xval_star = office_episodes[~office_episodes['guest_stars'].isna()]['episode_number']
yval_star = office_episodes[~office_episodes['guest_stars'].isna()]['viewership_mil']
# generate scatter plot
sinput_reg = [sizes[idx] for idx in idx_reg]
sinput_star = [sizes[idx] for idx in idx_star]
cinput_reg = [colors[idx] for idx in idx_reg]
cinput_star = [colors[idx] for idx in idx_star]
scatplt_all = plt.scatter(xval_reg,yval_reg,color = cinput_reg, s = sinput_reg, marker = '.')
scatplt_star = plt.scatter(xval_star,yval_star,color = cinput_star, s = sinput_star, marker = '*')
plt.title('Popularity, Quality, and Guest Appearances on the Office',fontsize=25)
plt.xlabel('Episode Number',fontsize=20)
plt.ylabel('Viewership (Millions)',fontsize=20)
plt.rcParams['figure.figsize'] = [11, 7]
plt.show()

# Determine the office episode with the highest viewership
top_stars = office_episodes.loc[office_episodes['viewership_mil'].idxmax(),'guest_stars']

list_top_stars = top_stars.split(',')
top_star = list_top_stars[-1].strip()
print(top_star + " appeared in the most watched Office episode.")