Home »
Python »
Python Programs
Scaling numbers column by column with pandas
Given a pandas dataframe, we have to scale numbers column by column with pandas.
By Pranit Sharma Last updated : September 30, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Scale numbers column by column with pandas
Scaling numbers is a practice of pre-processing technique which is a process used in machine learning models to generalize the independent features present in the data in a fixed range. When we apply this process to Python sequence like Pandas Series, it results in a new sequence such that your entire values in a series falls under a range. For example, if the range is (0 ,1 ) our entire data within that column will be in the range 0,1 only.
We are going to have to create a DataFrame, where we will be having two columns A and B and we will set the maximum value of A as 1 and minimum values as 0 whereas we will set the maximum value of B as 0 and minimum values as 1.
Python program to scale numbers column by column with pandas
# Importing pandas package
import pandas as pd
# Importing methods from sklearn
from sklearn.preprocessing import MinMaxScaler
# Creating a dictionary
d = {
'A':[20,30,32,34,54,56],
'B':[18,93,28,56,83,39]
}
# Creating DataFrame
df = pd.DataFrame(d)
# Display the DataFrame
print("Original DataFrame:\n",df,"\n")
# Scaling columns
scaler = MinMaxScaler()
scaled_df = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)
# Display result
print("Result:\n",scaled_df)
Output
The output of the above program is:
Python Pandas Programs »