标准化 pandas 数据框的列 [英] Normalize columns of pandas data frame
本文介绍了标准化 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屋!
查看全文