Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

Customer Churn Analysis

Analyze churn patterns, segment churn rates, and build a simple baseline model-ready dataset.

Goal

Given a customer dataset with churn (0/1), analyze:

  • Overall churn rate
  • Churn by segment (plan, region)
  • Numeric differences (tenure, usage)

Analysis pipeline

diagram Customer churn analysis pipeline mermaid
From raw customer records to the segments most likely to leave.

Step 1: Load

Python
import pandas as pd
 
df = pd.read_csv("data/churn.csv")
print(df.shape)
print(df.head())

Step 2: Churn rate

Python
rate = df["churn"].mean()
print("Churn rate:", rate)

Step 3: Churn by category

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
sns.barplot(data=df, x="plan", y="churn")
plt.title("Churn rate by plan")
plt.tight_layout()
plt.show()

Step 4: Numeric differences

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
sns.boxplot(data=df, x="churn", y="tenure")
plt.title("Tenure vs churn")
plt.tight_layout()
plt.show()

Visualize it

sketch Stayed vs churned p5.js
A donut chart makes the overall churn rate easy to read at a glance.

Step 5: Create a simple model-ready dataset

  • Handle missing values
  • Encode categories
  • Split train/test

This connects back to Phase 4 preprocessing.

Deliverable

Write insights:

  • Which plan/segment churns more?
  • Which features differ strongly?
  • What interventions might reduce churn?

Check your understanding

quizCheck yourself
  1. What does df["churn"].mean() return for a 0/1 column?

    Show answer

    A — The fraction of customers who churned — The mean of a 0/1 column is a rate.

  2. How do you get a churn rate per plan?

    Show answer

    A — df.groupby("plan")["churn"].mean() — The same mean pattern applied per group gives a rate for each plan.

  3. Why does the project use a boxplot of tenure by churn?

    Show answer

    A — To compare how a numeric feature differs between churned and retained customers — Tenure is numeric and churn splits customers into two groups, so a box plot shows the difference.

  4. What does preparing a model-ready dataset involve here?

    Show answer

    A — Handle missing values, encode categories and split train/test — This links back to the Phase 4 preprocessing steps.

🧪 Try It Yourself

Exercise 1 – Overall churn rate

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Churn rate by plan

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Compare tenure between groups

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did