Predictive Hacks

How to apply a Sklearn scaler by rows in Pandas Dataframe

Let’s say that we want to apply the MinMaxScaler from the Sklearn in a pandas Data Frame by row and not by column which is the default.

import pandas as pd
from sklearn.preprocessing import MinMaxScaler
#for this post we will use MinMaxScaler
scaler=MinMaxScaler()

df=pd.DataFrame({'A':[10,0.3,141],'B':[13,0.5,110],'C':[11,0.1,107]})
df
       A      B      C
0   10.0   13.0   11.0
1    0.3    0.5    0.1
2  141.0  110.0  107.0
scaled=pd.DataFrame(scaler.fit_transform(df.T).T,columns=df.columns)
scaled
     A         B         C
0  0.0  1.000000  0.333333
1  0.5  1.000000  0.000000
2  1.0  0.088235  0.000000

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s