Normalize columns of a dataframe
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Normalizing DataFrame columns means rescaling features so they are comparable across different ranges. The best method depends on what "normalize" means in your context, because people often use that word for several different transformations such as min-max scaling, z-score standardization, or row-wise vector normalization.
Min-Max Scaling with Pandas
Min-max scaling transforms each column into a fixed range, commonly 0 to 1.
This is simple and works well when you want bounded values, but it is sensitive to outliers because the minimum and maximum determine the whole scale.
Standardization with Mean and Standard Deviation
A different and often more useful transformation is z-score standardization:
This centers each column around zero and scales it by standard deviation. It is common in machine learning pipelines and often preferred for methods that assume centered numerical features.
Normalize Only Selected Columns
Real DataFrames often contain text columns, identifiers, or target labels that should not be scaled. Select the numeric columns you actually want to normalize.
Being explicit about columns is safer than applying arithmetic to the whole DataFrame and hoping the non-numeric fields behave.
Using scikit-learn Scalers
For reusable training pipelines, scikit-learn is often the better tool because it stores fitted scaling parameters.
This becomes important when you need to transform training data and later apply the exact same scaling to validation or production data.
Fit on Training Data Only
In machine learning, do not compute normalization statistics on the full dataset before splitting. Fit the scaler on training data, then transform validation and test data with those same parameters. Otherwise, information from held-out data leaks into training.
That rule matters more than the specific scaling formula. Many preprocessing bugs come from data leakage rather than from a wrong normalization equation.
Row Normalization Is a Different Operation
Sometimes people say "normalize the dataframe" when they actually mean normalize each row vector to unit length. That is different from column scaling and is used in some similarity or text-processing workflows.
For ordinary tabular feature preprocessing, column-wise scaling is usually what you want. Mixing row normalization and column normalization under the same label is a common source of confusion, so it is worth naming the intended transformation explicitly.
That naming clarity helps teams avoid subtle bugs in feature engineering pipelines. Two developers can both claim they "normalized the data" while having applied completely different transforms unless the method is stated precisely.
Common Pitfalls
- Using the word "normalize" without deciding whether you mean min-max scaling, standardization, or another transformation.
- Scaling identifier or label columns that should remain untouched.
- Applying a scaler to the full dataset before train-test splitting and leaking information.
- Ignoring outliers when using min-max scaling.
- Refitting the scaler separately on production or test data instead of reusing training parameters.
Summary
- Column normalization can mean different scaling strategies, so define the goal first.
- Min-max scaling maps values into a bounded range such as
0to1. - Standardization centers columns and scales by standard deviation.
- Apply normalization only to the columns that should be scaled.
- In modeling workflows, fit scaling parameters on training data and reuse them elsewhere.
Related reading
- Normalize data before or after split of training and testing data?
- Normalized Cross-Correlation in Python
- Normalizing feature values for SVM
- Normalizing to 0,1 vs -1,1
- Not able to import tensorflow_datasets module in jupyter notebook
- NotImplementedError Cannot convert a symbolic Tensor lstm_2/strided_slice0 to a numpy array. T
- np.mean vs np.average in Python NumPy?
- Numpy - add row to array
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.