标准化 pandas 中的数据 [英] Normalize data in pandas
本文介绍了标准化 pandas 中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个熊猫数据框df
:
Suppose I have a pandas data frame df
:
我想计算数据框的列均值.
I want to calculate the column wise mean of a data frame.
这很简单:
df.apply(average)
然后是列式范围 max(col) - min(col).这又很简单:
then the column wise range max(col) - min(col). This is easy again:
df.apply(max) - df.apply(min)
现在对于每个元素,我想减去其列的平均值并除以其列的范围.我不知道该怎么做
Now for each element I want to subtract its column's mean and divide by its column's range. I am not sure how to do that
非常感谢任何帮助/指示.
Any help/pointers are much appreciated.
推荐答案
In [92]: df
Out[92]:
a b c d
A -0.488816 0.863769 4.325608 -4.721202
B -11.937097 2.993993 -12.916784 -1.086236
C -5.569493 4.672679 -2.168464 -9.315900
D 8.892368 0.932785 4.535396 0.598124
In [93]: df_norm = (df - df.mean()) / (df.max() - df.min())
In [94]: df_norm
Out[94]:
a b c d
A 0.085789 -0.394348 0.337016 -0.109935
B -0.463830 0.164926 -0.650963 0.256714
C -0.158129 0.605652 -0.035090 -0.573389
D 0.536170 -0.376229 0.349037 0.426611
In [95]: df_norm.mean()
Out[95]:
a -2.081668e-17
b 4.857226e-17
c 1.734723e-17
d -1.040834e-17
In [96]: df_norm.max() - df_norm.min()
Out[96]:
a 1
b 1
c 1
d 1
这篇关于标准化 pandas 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文