DataFrame
Normalization
Data Processing
Pandas
Data Analysis

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.

Practice ML system design

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "age": [20, 30, 40],
5    "income": [30000, 50000, 70000],
6})
7
8scaled = (df - df.min()) / (df.max() - df.min())
9print(scaled)

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:

python
standardized = (df - df.mean()) / df.std(ddof=0)
print(standardized)

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.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "city": ["A", "B", "C"],
5    "age": [20, 30, 40],
6    "income": [30000, 50000, 70000],
7})
8
9cols = ["age", "income"]
10df[cols] = (df[cols] - df[cols].min()) / (df[cols].max() - df[cols].min())
11print(df)

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.

python
1import pandas as pd
2from sklearn.preprocessing import MinMaxScaler
3
4df = pd.DataFrame({
5    "age": [20, 30, 40],
6    "income": [30000, 50000, 70000],
7})
8
9scaler = MinMaxScaler()
10df[["age", "income"]] = scaler.fit_transform(df[["age", "income"]])
11print(df)

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 0 to 1.
  • 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.