我正在寻找一种方法来查找python数据框中每列的方法,并用该列的平均值减去该列。假设,
df = pd.DataFrame({'a': [1.5, 2.5], 'b': [0.25, 2.75], 'c': [1.25, 0.75]})
我想找到每列的平均值,它将返回(2,1.5,1)
并分别从1,2,3列中减去值。
会给出((-0.5,0.5),(-1.25, 1.5), (0.25,-0.25))
有人可以帮我这么做吗?
由于
答案 0 :(得分:9)
您只需使用In [18]: %timeit [5 * i for i in my_list]
463 ns ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [19]: %timeit list(map((5).__mul__, my_list))
784 ns ± 10.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [20]: %timeit [5 * i for i in my_list * 100000]
20.8 ms ± 115 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [21]: %timeit list(map((5).__mul__, my_list * 100000))
30.6 ms ± 169 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [24]: arr = np.array(my_list * 100000)
In [25]: %timeit arr * 5
899 µs ± 4.98 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
<强>代码:强>
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()
答案 1 :(得分:3)
试试这个:
>>> df
a b c
0 1.5 0.25 1.25
1 2.5 2.75 0.75
>>> df.columns
Index([u'a', u'b', u'c'], dtype='object')
>>> for x in df.columns:
... df[x] = df[x] - df[x].mean()
...
>>> df
a b c
0 -0.5 -1.25 0.25
1 0.5 1.25 -0.25
Pythonic方式:
>>> df - df.mean()
a b c
0 -0.5 -1.25 0.25
1 0.5 1.25 -0.25