Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum all values in a dataframe

Tags:

python

pandas

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.

I have tried df.sum() but that only sums by column.

like image 882
Stacey Avatar asked Sep 01 '15 20:09

Stacey


2 Answers

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:

>>> %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
like image 165
DSM Avatar answered Nov 01 '22 17:11

DSM


Just sum the column sums:

df.sum().sum()
like image 41
Alexander Avatar answered Nov 01 '22 16:11

Alexander