如何将Pandas数据框中的所有数值相加,得到一个值[英] How to sum up all the numeric values in Pandas Data Frame to yield one value

本文是小编为大家收集整理的关于如何将Pandas数据框中的所有数值相加,得到一个值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有以下数据框架:

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"]]

看起来像这样:

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

我要做的是总结所有数值,该值应产生一个值

224

做什么方法?

我尝试过,但给出了两个值:

In [134]: source_df.sum(numeric_only=True)
Out[134]:
cell1     22
cell2    202
dtype: int64

推荐答案

您需要再次致电sum().示例 -

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

其他推荐答案

由于source_df.sum(numeric_only=True)返回一系列总和,您可以简单地将返回系列中的所有值与另一个总和():

:

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

输出产生一个值:

224

另外,您可以通过手动循环并逐步循环

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

本文地址:https://www.itbaoku.cn/post/1728211.html

问题描述

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

推荐答案

You need to call sum() again. Example -

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

其他推荐答案

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 = 0
for v in source_df.sum(numeric_only=True):
    total += v
print(total)