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.
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
Just sum the column sums:
df.sum().sum()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With