Exploring Netflix Data in Python. Exploratory Data Analysis of Netflix... | by Sadrach Pierre, Ph.D. | Towards Data Science

Flixable is a search engine for video streaming services that offers a complete list of movies and shows streaming on Netflix. The search engine released the Netflix Movies and TV Shows data set, which includes the complete list of movies and shows available in 2019.
In this post, we will perform exploratory data analysis on Netflix Movies and TV Shows data set. The data can be found here.
Let’s get started!
First, let’s import Pandas and read the data into a data frame:
import pandas as pd df = pd.read_csv("netflix_titles.csv")
Next, let’s print the list of columns:
print(list(df.columns))
We can also take a look at the number of rows in the data:
print("Number of rows: ", len(df))
Let’s print the first five rows of the data:
print(df.head())
We can see that there are several categorical columns. Let’s define a function that takes as input a data frame, column name, and limit. When called, it prints a dictionary of categorical values and how frequently they appear:
def return_counter(data_frame, column_name, limit):   from collections import Counter    print(dict(Counter(data_frame[column_name].values).most_common(limit)))
Let’s apply our function to the ‘country’ column and limit our results to the five most common values:
return_counter(df, 'country', 5)
As we can see, we have 2,032 titles in the US, 777 in India, 476 missing country values, 348 in the UK, and 176 in Japan.
Let’s apply our function to the ‘director’ column, upon dropping missing values:
df['director'].dropna(inplace = True)return_counter(df, 'director', 5)
Now, let’s look at the titles from the most common directors ‘Raul Campos’ and ‘Jan Suter’:
df_d1 = df[df['director'] =='Raúl Campos, Jan Suter']print(set(df_d1['title']))
And the countries:
print(set(df_d1['country']))
We see that these are titles from Colombia, Chile, Argentina, and Mexico. Let’s do the same for Marcus Raboy:
df_d2 = df[df['director'] =='Marcus Raboy']print(set(df_d2['title']))
Now we’ll analyze movie durations. First, we need to filter the data set to only include movie titles:
df = df[df['type'] =='Movie']
Let’s then print the set of movie durations:
print(set(df['duration']))
We see that all values are reported as strings and the duration is measured in minutes. Let’s remove ‘min’ from the string values and convert the result to integers:
df['duration'] = df['duration'].map(lambda x: x.rstrip('min')).astype(int)print(set(df['duration']))
Next, it would be useful to generate summary statistics from numerical columns like ‘duration’. Let’s define a function that takes a data frame, a categorical column, and a numerical column. The mean and standard deviation of the numerical column for each category is stored in a data frame and the data frame is sorted in descending order according to the mean. This is useful if you want to quickly see if certain categories have higher or lower mean and/or standard deviation values for a particular numerical column.
def return_statistics(data_frame, categorical_column, numerical_column):    mean = []    std = []    field = []    for i in set(list(data_frame[categorical_column].values)):        new_data = data_frame[data_frame[categorical_column] == i]        field.append(i)        mean.append(new_data[numerical_column].mean())        std.append(new_data[numerical_column].std())    df = pd.DataFrame({'{}'.format(categorical_column): field, 'mean {}'.format(numerical_column): mean, 'std in {}'.format(numerical_column): std})    df.sort_values('mean {}'.format(numerical_column), inplace = True, ascending = False)    df.dropna(inplace = True)    return df
Let’s call our function with categorical column ‘listed_in’ and numerical column ‘duration’:
stats = return_statistics(df, 'listed_in', 'duration')print(stats.head(15))
Next, we will use boxplots to visualize the distribution in numeric values based on the minimum, maximum, median, first quartile, and third quartile. If you are unfamiliar with them, take a look at the article Understanding Boxplots.
Similar to the summary statistics function, this function takes a data frame, categorical column, and numerical column and displays boxplots for the most common categories based on the limit:
def get_boxplot_of_categories(data_frame, categorical_column, numerical_column, limit):    import seaborn as sns    from collections import Counter    keys = []    for i in dict(Counter(df[categorical_column].values).most_common(limit)):        keys.append(i)    print(keys)    df_new = df[df[categorical_column].isin(keys)]    sns.set()    sns.boxplot(x = df_new[categorical_column], y =      df_new[numerical_column])
Let’s generate boxplots for ‘duration’ in the 5 most commonly occurring ‘listed in’ categories:
get_boxplot_of_categories(df, 'listed_in', 'duration', 5)
Finally, let’s define a function takes a data frame and a numerical column as input and displays a histogram:
def get_histogram(data_frame, numerical_column):    df_new = data_frame    df_new[numerical_column].hist(bins=100)
Let’s call the function with the data frame and generate a histogram from ‘duration’:
get_histogram(df, 'duration')
I will stop here but please feel free to play around with the data and code yourself.
To recap, I went over several methods for analyzing the Netflix Movies and TV Shows data set. This included defining functions for generating summary statistics like the mean, standard deviation, and counts for categorical values. We also defined functions for visualizing data with boxplots and histograms. I hope this post was interesting. The code from this post is available on GitHub. Thank you for reading!