Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum up all the numeric values in Pandas Data Frame to yield one value

Tags:

python

pandas

I have the following data frame:

import pandas as pd
source_df = pd.DataFrame({ 'gene':["foo","bar","qux","woz"], 'cell1':[5,9,1,7], 'cell2':[12,90,13,87]})
source_df = source_df[["gene","cell1","cell2"]]

It looks like this:

In [132]: source_df
Out[132]:
  gene  cell1  cell2
0  foo      5     12
1  bar      9     90
2  qux      1     13
3  woz      7     87

What I want to do is to sum all the numeric values, that should yield a single value

224

What's the way to do it?

I tried this but give two values instead:

In [134]: source_df.sum(numeric_only=True)
Out[134]:
cell1     22
cell2    202
dtype: int64
like image 848
neversaint Avatar asked Jan 08 '23 16:01

neversaint


1 Answers

You need to call sum() again. Example -

In [5]: source_df.sum(numeric_only=True).sum()
Out[5]: 224
like image 103
Anand S Kumar Avatar answered Jan 29 '23 11:01

Anand S Kumar