python - 规范化 pandas 中的数据

标签 python pandas numpy

假设我有一个 pandas 数据框 df:

我想计算数据框的列平均值。

这很简单:

df.apply(average) 

然后按列范围 max(col) - min(col)。这又很容易:

df.apply(max) - df.apply(min)

现在对于每个元素,我想减去其列的平均值并除以其列的范围。我不知道该怎么做

非常感谢任何帮助/指针。

最佳答案

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

关于python - 规范化 pandas 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12525722/

相关文章:

python - 在具有共享 x 轴的子图中拟合对数正态分布的直方图

Pandas 将滚动功能与 Group By 相结合

python - (x-y)^2 的 matplotlib 曲面图

python - NumPy 是否有相当于 Matlab 缓冲区的功能?

python - 如何在没有函数的脚本上使用 pytest

Python:为什么我会收到 WinError 32?

python - PyQt4 和写入串行是否需要多线程

python - reshape python连接文本不规则数据框

python - 根据组比较过滤 pandas 数据框组

python - 我需要一种快速的方法来循环遍历 Python 中图像/堆栈的像素