Skip to content

Netflix! What started in 1997 as a DVD rental service has since exploded into one of the largest entertainment and media companies.

Given the large number of movies and series available on the platform, it is a perfect opportunity to flex your exploratory data analysis skills and dive into the entertainment industry. Our friend has also been brushing up on their Python skills and has taken a first crack at a CSV file containing Netflix data. They believe that the average duration of movies has been declining. Using your friends initial research, you'll delve into the Netflix data to see if you can determine whether movie lengths are actually getting shorter and explain some of the contributing factors, if any.

You have been supplied with the dataset netflix_data.csv , along with the following table detailing the column names and descriptions:

The data

netflix_data.csv

ColumnDescription
show_idThe ID of the show
typeType of show
titleTitle of the show
directorDirector of the show
castCast of the show
countryCountry of origin
date_addedDate added to Netflix
release_yearYear of Netflix release
durationDuration of the show in minutes
descriptionDescription of the show
genreShow genre
# Importing pandas and matplotlib
import pandas as pd
import matplotlib.pyplot as plt

# Start coding!
netflix_df = pd.read_csv('netflix_data.csv')
netflix_subset = netflix_df[netflix_df['type'] == "Movie"]
netflix_movies = netflix_subset[["title", "country", "genre", "release_year", "duration"]]
short_movies = netflix_movies[netflix_movies.duration < 60]

Inspecting short movies

# Inspecting results to find possible contribution factors - look at correlation with a year
corr_year = short_movies['release_year'].corr(short_movies['duration'])
corr_year
# Plot the average duration per year 
short_movies['avg_dur_year']=short_movies.groupby('release_year')['duration'].transform('mean')
short_movies.sort_values(by='release_year', ascending=False, inplace=True)

plt.figure(figsize=(12, 6))
plt.plot(short_movies['release_year'], short_movies['avg_dur_year'])
plt.xlabel('year')
plt.ylabel('Duration')
plt.title('Average Movie Duration per Year')
plt.xticks(rotation=90)

plt.show
short_movies.isnull().sum()
# Plot average movie duration per genre
# Calculate the averages
short_movies['avg_dur_genre'] = short_movies.groupby('genre')['duration'].transform('mean')

# Sort values by average duration for more representative plot
short_movies.sort_values(by='avg_dur_genre', ascending=False, inplace=True)


plt.figure(figsize=(8, 8))
plt.bar(short_movies['genre'], short_movies['avg_dur_genre'])
plt.xlabel('Genre')
plt.ylabel('Duration')
plt.title('Average Movie Duration per Genre')
plt.xticks(rotation=90)

plt.show
# Plot movie duration per country 

# Impute null values with another category - 'no country'
short_movies['country'].fillna('No country',inplace=True)

# Calculate averages per country and sort by those values
short_movies['avg_dur_country'] = short_movies.groupby('country')['duration'].transform('mean')
short_movies.sort_values(by= 'avg_dur_country',ascending = False,  inplace = True)

# Make a plot
plt.figure(figsize=(8, 6))
plt.barh(short_movies['country'], short_movies['avg_dur_country'])
plt.xlabel('Country')
plt.ylabel('Duration')
plt.title('Average Movie Duration per Genre')
plt.xticks(rotation=90)

plt.show

Assign colours for given genres using foor loop

# Initialise empty list to store the colors
colors = []

# Iterate through rows of netflix_movies 

for index, row in netflix_movies.iterrows():
    genre=row['genre']
    
    # Assign the colors respectively
    if genre =='Children':
        colors.append('green')
    
    elif genre == 'Documentaries':
        colors.append('blue')
        
    elif genre == 'Stand-Up':
        colors.append('red')
    
    else:
        colors.append('grey')