Guided Notes
Transform: Dummy Variables & Text Cleaning
Cleaning categorical text with replace and regular expressions, then one-hot encoding with get_dummies.
What are dummy variables?
Slide 1 / 5Machine learning models need numbers, but many columns hold categories: a project's sector might be Energy, Transportation, or Health.
Dummy variables (one-hot encoding) turn one categorical column into several 0/1 columns β one per category. A project in Energy gets 1 in the Energy column and 0 in all others.
pd.get_dummies(df['sector']) does it in one call.
| Project | Energy | Transportation | Health |
|---|---|---|---|
| P1 | 1 | 0 | 0 |
| P2 | 0 | 1 | 0 |
| P3 | 0 | 0 | 1 |
