我正在寻找一种方法来查找python数据帧中每一列的平均值,然后将该列的平均值减去该列。
假设我有:
df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})
我想找到的每一列,这将返回的平均值(2,1.5,1)
和列减去值a
,b
并c
分别。
这会给 ((-0.5,0.5),(-1.25, 1.5), (0.25,-0.25))
有人可以帮我吗?
谢谢
您可以简单地使用 mean function of pandas
代码:
import pandas as pd
df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})
print "The data frame"
print df
print "The mean value"
print df.mean()
print "The value after subraction of mean"
print df -df.mean()
输出:
The data frame
a b c
0 1.5 0.25 1.25
1 2.5 2.75 0.75
The mean value
a 2.0
b 1.5
c 1.0
dtype: float64
The value after subraction of mean
a b c
0 -0.5 -1.25 0.25
1 0.5 1.25 -0.25
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句