This topic will cover the importance of data wrangling in the data preprocessing pipeline, including understanding messy data, identifying data quality issues, and the role of data wrangling in preparing data for analysis.
Introduction to Data Wrangling
The essential first step in turning raw data into actionable insights.
| Term | Meaning |
|---|---|
| Data Wrangling (also called data munging, data cleaning, or data preprocessing) | The process of transforming raw, messy, or unstructured data into a tidy, analysis‑ready format. |
| Goal | Reduce errors, handle inconsistencies, and create a reproducible dataset that can be explored, modeled, and visualized with confidence. |
| Task | Typical Operations | Tools / Functions (Python) |
|---|---|---|
| Missing Data | - Identify (isnull) - Impute (mean, median, mode, K‑NN) - Flag or drop rows/columns |
pandas.isna(), SimpleImputer, fillna() |
| Duplicates | - Detect (duplicated) - Remove or consolidate |
drop_duplicates() |
| Data Types | - Cast (astype) - Parse dates ( to_datetime) - Convert categorical strings to category |
astype(), pd.to_datetime() |
| String Cleaning | - Trim whitespace, lower‑case - Regex replace, extract patterns |
str.strip(), str.replace(), str.extract() |
| Outliers | - Visual inspection (boxplot) - Statistical rules (IQR, Z‑score) - Cap or remove |
np.percentile(), scipy.stats.zscore |
| Reshaping | - Pivot / unpivot - Melt, stack, wide ↔ long |
pivot(), melt(), stack() |
| Feature Engineering | - Create ratios, flags, rolling windows - Encode categorical variables (one‑hot, label) |
pd.get_dummies(), OrdinalEncoder |
| Joining Data | - Merge tables on keys, handle many‑to‑many - Concatenate datasets |
merge(), concat() |
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
# 1️⃣ Load raw data
df = pd.read_csv('sales_raw.csv')
# 2️⃣ Profile
print(df.info())
print(df.describe(include='all'))
# 3️⃣ Clean