pandas 对数据框中的所有值求和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32340689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum all values in a dataframe
提问by Stacey
I am trying to sum all the values in a dataframe into one number.
我正在尝试将数据框中的所有值汇总为一个数字。
So for example with the dataframe
因此,例如使用数据框
BBG.XAMS.FUR.S_pnl_pos_cost BBG.XAMS.MT.S_pnl_pos_cost
date
2015-03-23 -0.674996 -0.674997
2015-03-24 82.704951 11.868748
2015-03-25 -11.027327 84.160210
2015-03-26 228.426675 -131.901556
2015-03-27 -99.744986 214.579858
I would like the value 377.71658 returned.
我希望返回值 377.71658。
I have tried df.sum() but that only sums by column.
我试过 df.sum() 但它只按列求和。
回答by DSM
I would do
我会做
>>> df.values.sum()
377.71658000000002
which drops down to the underlying numpy array, and is likely to be the fastest, if the frame is all-numeric. But there are lots of other options:
它下降到底层的 numpy 数组,并且可能是最快的,如果框架是全数字的。但还有很多其他选择:
>>> %timeit df.values.sum()
100000 loops, best of 3: 6.27 μs per loop
>>> %timeit df.sum().sum()
10000 loops, best of 3: 109 μs per loop
>>> %timeit df.unstack().sum()
1000 loops, best of 3: 233 μs per loop
>>> %timeit df.stack().sum()
1000 loops, best of 3: 190 μs per loop
回答by Alexander
Just sum the column sums:
只需总结列总和:
df.sum().sum()
回答by U10-Forward
If data-frame contains non-numeric data, and want to just get sum of integers, do:
如果数据框包含非数字数据,并且只想获取整数总和,请执行以下操作:
df.sum(numeric_only=True).sum()

