# Customer Churn Data Cleaning

> Cleaning a 50,000-row e-commerce churn dataset in pandas: tiered missing-value handling with missing flags, logic checks + IQR winsorizing, and ordinal / one-hot / frequency encoding. Every number in the report comes from the script's own output.

- Result: 50,000 × 25 → 49,832 × 44, only 168 rows dropped
- Stack: Python, Pandas, Data Viz

A Kaggle e-commerce customer churn dataset with 50,000 rows, 25 columns and a churn rate of about 29%. The assignment had three parts — missing values, outliers and categorical encoding — each detected before being treated, ending with a clean dataset and a report.

## Problem

A Kaggle e-commerce customer churn dataset with 50,000 rows and 25 columns and a churn rate of about 29%. The assignment: handle missing values, outliers and categorical encoding — detecting before treating each one — and deliver a clean dataset and a report.

My role: Group assignment for the Big Data Business Analytics and Decision Making course.

## Key decisions

1. **Treat missing values by how much is missing, instead of dropna**: Dropping would lose 64% of the rows; under 1% drops the row, 1–5% fills the median, and 5% or more fills the median plus a missing-flag column (missing app usage hours might mean the app was never installed).
2. **Fill with the median rather than the mean**: skew() confirmed that most behavioural columns are right-skewed.
3. **Outliers: business sense first, statistics second**: Logical errors such as age 5, age 200 or negative purchase counts only set that cell to missing; extreme values beyond 1.5×IQR are capped at the bounds (winsorised) instead of deleted — big spenders are a real and important customer group.
4. **Pick the encoding to fit each category**: Signup quarter has an order → ordinal mapping; gender and country have few values → one-hot (drop_first); city has 40 values → frequency encoding, to avoid 40 extra columns.

## Trade-offs

- Statistics (median, IQR) were computed on the full dataset; for modelling they should come from the training set only to avoid data leakage (the report says so)

## Results

- Only 168 of 50,000 rows were dropped; everything else was imputed or winsorised and kept
- Every number in the report is actual program output

## Missing values

- 14 columns had missing values (0.34% to 12%). Dropping them would lose 64% of the rows, so they were treated by level instead
- Under 1%: drop the row; 1–5%: fill the median; 5% or more: fill the median and add a missing-flag column (missing app usage hours might mean the app was never installed)
- Median rather than mean: skew() showed most behavioural columns are right-skewed

## Outliers

- First, business sense catches logical errors: age 5, age 200, negative purchase counts, ratios over 100% — 327 cells in total, each set to missing without dropping the whole row
- Then 1.5×IQR catches statistical extremes, capped at the bounds (winsorised) instead of deleted — high-value customers are a real and important group
- Rating-style columns (such as the number of payment methods, 1–5) and columns with IQR = 0 aren't capped; bounds on integer columns are rounded first so you never get "11.5 purchases"

## Encoding

- Signup quarter has an order → ordinal mapping 1–4
- Gender and country have few values → one-hot (drop_first to avoid the dummy-variable trap)
- City has 40 values → frequency encoding, avoiding 40 one-hot columns
- The report also states the limitation: statistics were computed on the full dataset, and for modelling they should come from the training set only to avoid data leakage

> Group assignment for the Big Data Business Analytics and Decision Making course. The screenshot shows box plots produced by the code (six columns, before and after).

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/projects/churn-preprocessing
