首页 > 解决方案 > 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.

标签: pythonpython-3.xdataframescikit-learn

解决方案


Sklearn 适用于 panda 数据帧和 numpy 数组,当数据帧不支持时,numpy 数组允许进行一些基本的矩阵转换。

您可以将数据框转换为 numpy 数组,vectors = df.values. 然后转置数组,按列缩放转置后的数组,将其转回

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

并将其转换为数据框scaled_df = pd.DataFrame(data = scaled_rows, columns = df.columns)


推荐阅读