Editor

Build the Customer DataFrame

Advanced · Write the code · from Predict Customer Churn: End-to-End Data Analysis (with sklearn)

From the lesson: Step 1: Create the Customer Dataset

Every data project starts with data. In a real company, you'd pull this from a database. Here, we'll build a realistic dataset as a pandas DataFrame. Our fictional telecom company tracks each customer's tenure, monthly charges, contract type, and whether they churned.

Read this part of the lesson

Create a pandas DataFrame called customers with at least 20 rows and these columns: customer_id (integers 1–20), tenure_months (integers), monthly_charges (floats or ints), contract (strings: 'Month', 'Year', or 'TwoYear'), and churned (0 or 1). Print the shape of the DataFrame and the churn rate (mean of the churned column) formatted as: Churn rate: X.XX.

import pandas as pd
# Create a customer dataset with at least 20 rows
# Columns: customer_id, tenure_months, monthly_charges, contract, churned

More Churn Prediction exercises

All Python exercises · 50 practice problems · Practical Projects quiz