1. Welcome!
.
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:
- 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 libraries : matplotlib and pandas
import pandas as pd
import matplotlib.pyplot as plt
#use pandas to read the CSV file as a dataframe and assign it to a variable
office_df = pd.read_csv('datasets/office_episodes.csv')
list(office_df)
#create a empyt list for colors
colors = []
#create a for loop to append the colors into the list
for lab, row in office_df.iterrows():
if row['scaled_ratings'] < 0.25 :
colors.append('red')
elif row['scaled_ratings'] < 0.50:
colors.append('orange')
elif row['scaled_ratings'] < 0.75:
colors.append('lightgreen')
else :
colors.append('darkgreen')
print(colors[0:10])
#create a sizes empty list for guest and non-guest episodes
sizes = []
#create a for loop to fill in the sizes in the list
for lab, row in office_df.iterrows():
if row['has_guests'] == True :
sizes.append(250)
else :
sizes.append(25)
print(sizes[0:10])
#add sizes and colors as columns into the dataframe
office_df['colors'] = colors
office_df['sizes'] = sizes
list(office_df)
#create fig object
fig =plt.figure()
#use the figure size parameters given
plt.rcParams['figure.figsize'] = [11,7]
#create scatterplot function and add color parameter and size parameter
plt.scatter(office_df['episode_number'],
office_df['viewership_mil'],
c=colors,
s=sizes)
#create title and labels
plt.title('Popularity, Quality, and Guest Appearances on the Office')
plt.xlabel('Episode Number')
plt.ylabel('Viewership (Millions)')
#create 2 new dataframe subsets, one with guests and another with non-guests
guest_df = office_df[office_df['has_guests'] == True]
non_guest_df =office_df[office_df['has_guests'] == False]
#create fig object
fig =plt.figure()
#use the figure size parameters given
plt.rcParams['figure.figsize'] = [11,7]
#create new scatter plot with the 2 dataframes and create a marker parameter for the dataframe with guests stars
plt.scatter(non_guest_df['episode_number'],
non_guest_df['viewership_mil'],
c=non_guest_df['colors'],
s=non_guest_df['sizes'])
plt.scatter(guest_df['episode_number'],
guest_df['viewership_mil'],
c=guest_df['colors'],
s=guest_df['sizes'],
marker ='*')
plt.title('Popularity, Quality, and Guest Appearances on the Office')
plt.xlabel('Episode Number')
plt.ylabel('Viewership (Millions)')
# subset from a boolian dataframe the stars in the episode with the highest viewership
star_office_df =office_df[office_df['viewership_mil'] > 20]['guest_stars']
top_star = 'Jack Black'