thinwybk
thinwybk

Reputation: 4753

pandas dataframe rows scaling with sklearn

How can I apply a sklearn scaler to all rows of a pandas dataframe. The question is related to pandas dataframe columns scaling with sklearn. How can I apply a sklearn scaler to all values of a row?

NOTE: I know that for feature scaling it's normal to have features in columns and scaling features column wise like in the refenced other question. However I'd like to use sklearn scalers for preprocessing data for visualization where it's reasonable to scale row wise in my case.

Upvotes: 2

Views: 2979

Answers (1)

Alexis Benichoux
Alexis Benichoux

Reputation: 800

Sklearn works both with panda dataframes and numpy arrays, and numpy arrays allow some basic matrix transformations when dataframes don't.

You can transform the dataframe to a numpy array, vectors = df.values. Then transpose the array, scale the transposed array columnwise, transpose it back

scaled_rows = scaler.fit_transform(vectors.T).T

and convert it to dataframe scaled_df = pd.DataFrame(data = scaled_rows, columns = df.columns)

Upvotes: 1

Related Questions