SKLearn MinMaxScaler - scale specific columns only

Learn, how to scale specific columns only using sklearn MinMaxScaler method?
Submitted by Pranit Sharma, on October 15, 2022

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.

Scaling specific columns only using sklearn MinMaxScaler method

The sklearn is a library in python which allows us to perform operations like classification, regression, and clustering, and also it supports algorithms like the random forest, k-means, support vector machines, and many more on our data set. With a huge number of methods in this library, it is possible to apply these algorithms and make machine-learning models for different purposes.

MinMaxScaler() is one of the methods of sklearn library, which is used to transform the given values by scaling each value to a given range.

Here we are going to scale some specific columns in the pandas DataFrame?

Let us understand with the help of an example,

Python program to scale some specific columns in pandas DataFrame

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Importing minmaxscaler method from sklearn
from sklearn.preprocessing import MinMaxScaler

# Creating DataFrame
df = pd.DataFrame(np.random.randn(5, 3), index=list('abcde'), columns=list('xyz'))

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Creating an object for MinMaxScaler
mms = MinMaxScaler()

# Scaling secific columns
df[['x','z']] = mms.fit_transform(df[['x','z']])

# Display Modified DataFrame
print("Modified DataFrame:\n",df)

Output

Example: SKLearn MinMaxScaler - scale specific columns only

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.