Skip to content Skip to sidebar Skip to footer

How To Sum Up All The Numeric Values In Pandas Data Frame To Yield One Value

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[[

Solution 1:

You need to call sum() again. Example -

In [5]: source_df.sum(numeric_only=True).sum()
Out[5]: 224

Solution 2:

Since source_df.sum(numeric_only=True) returns a Series of sums, you can simply sum up all values in the returned series with another sum():

source_df.sum(numeric_only=True).sum()

output yields a single value:

224

Alternatively, you can loop thru and tally up the total manually

total = 0for v in source_df.sum(numeric_only=True):
    total += v
print(total)

Post a Comment for "How To Sum Up All The Numeric Values In Pandas Data Frame To Yield One Value"