Sum all values ​​in a data frame

I am trying to sum all the values ​​in a data frame by one number.

So for example using 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 to return the value 377.71658.

I tried df.sum (), but only the sum of the column.

Any help would be greatly appreciated.

+5
source share
2 answers

Just sum the sums of the columns:

 df.sum().sum() 
+3
source

I would do

 >>> df.values.sum() 377.71658000000002 

which goes down to the numpy base array and is likely to be the fastest if the frame is numeric. But there are many other possibilities:

 >>> %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 
+3
source

All Articles