规范化 pandas 数据框的列 [英] Normalize columns of pandas data frame

查看:30
本文介绍了规范化 pandas 数据框的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pandas 中有一个数据框,其中每列都有不同的值范围.例如:

I have a dataframe in pandas where each column has different value range. For example:

df:

A     B   C
1000  10  0.5
765   5   0.35
800   7   0.09

知道如何标准化每个值都在 0 到 1 之间的数据帧的列吗?

Any idea how I can normalize the columns of this dataframe where each value is between 0 and 1?

我想要的输出是:

A     B    C
1     1    1
0.765 0.5  0.7
0.8   0.7  0.18(which is 0.09/0.5)

推荐答案

您可以使用包 sklearn 及其相关的预处理实用程序来规范化数据.

You can use the package sklearn and its associated preprocessing utilities to normalize the data.

import pandas as pd
from sklearn import preprocessing

x = df.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df = pd.DataFrame(x_scaled)

有关更多信息,请查看 scikit-learn 关于预处理数据的文档:将特征缩放到一个范围.

For more information look at the scikit-learn documentation on preprocessing data: scaling features to a range.

这篇关于规范化 pandas 数据框的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆