Goal
Analyze the Netflix titles dataset to answer:
- Movies vs TV shows split
- Top countries producing content
- Content additions per year
- Popular genres/categories
Analysis pipeline
flowchart LR A["Raw titles
(CSV)"] --> B["Clean
(parse dates, split country/genre)"] B --> C["Explode
(one row per country/genre)"] C --> D["Visualize
(type split, top countries, yearly adds)"] D --> E["Conclude
(catalog trends)"]
Common columns (Netflix titles dataset)
type(Movie/TV Show)title,director,castcountry,date_added,release_yearrating,duration,listed_in
Step 1: Load and parse dates
import pandas as pd
df = pd.read_csv("data/netflix_titles.csv")
if "date_added" in df.columns:
df["date_added"] = pd.to_datetime(df["date_added"], errors="coerce")
print(df.shape)
print(df.head())Step 2: Clean multi-valued country/genre
The dataset often stores multiple values separated by commas.
# Countries
if "country" in df.columns:
countries = (
df.dropna(subset=["country"])
.assign(country=df["country"].str.split(","))
.explode("country")
)
countries["country"] = countries["country"].str.strip()
# Genres
if "listed_in" in df.columns:
genres = (
df.dropna(subset=["listed_in"])
.assign(genre=df["listed_in"].str.split(","))
.explode("genre")
)
genres["genre"] = genres["genre"].str.strip()Step 3: Movies vs TV shows
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))
sns.countplot(data=df, x="type")
plt.title("Movies vs TV Shows")
plt.tight_layout()
plt.show()Step 4: Top countries
import seaborn as sns
import matplotlib.pyplot as plt
if "country" in df.columns:
top_c = countries["country"].value_counts().head(10).reset_index()
top_c.columns = ["country", "count"]
plt.figure(figsize=(10, 4))
sns.barplot(data=top_c, x="count", y="country")
plt.title("Top 10 countries by number of titles")
plt.tight_layout()
plt.show()Step 5: Additions over time
import matplotlib.pyplot as plt
if "date_added" in df.columns:
yearly = df.dropna(subset=["date_added"]).groupby(df["date_added"].dt.year).size()
plt.figure(figsize=(8, 4))
plt.plot(yearly.index, yearly.values, marker="o")
plt.title("Titles added by year")
plt.xlabel("Year")
plt.ylabel("Count")
plt.tight_layout()
plt.show()Visualize it
Deliverable
Summarize 5โ10 insights:
- Which type dominates?
- Which countries dominate?
- Any clear growth periods?
- Which genres show up most?
Check your understanding
Why use str.split(",") followed by explode() on country and listed_in?
After exploding, value_counts counts one country or genre at a time.
Show answer
A โ One cell holds several comma-separated values, so each needs its own row to be counted correctly โ After exploding, value_counts counts one country or genre at a time.
Why strip whitespace after exploding?
Splitting on commas leaves leading spaces in the pieces.
Show answer
A โ Values like " India" and "India" would otherwise be counted as different countries โ Splitting on commas leaves leading spaces in the pieces.
How does the project compute titles added per year?
It uses groupby(df["date_added"].dt.year).size() after dropping rows without a date.
Show answer
A โ Group the non-null date_added values by its year and count rows โ It uses groupby(df["date_added"].dt.year).size() after dropping rows without a date.
What does errors="coerce" do in pd.to_datetime for date_added?
Bad or missing dates become NaT, which are then dropped before grouping.
Show answer
A โ Turns unparseable dates into NaT instead of raising โ Bad or missing dates become NaT, which are then dropped before grouping.
๐งช Try It Yourself
Exercise 1 โ Split a multi-value column
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Explode into one row per value
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Titles added per year
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.