如何对Pandas Data Frame中的所有数值求和以得出一个值 [英] How to sum up all the numeric values in Pandas Data Frame to yield one value

查看:161
本文介绍了如何对Pandas 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"]]

它看起来像这样:

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

这是怎么做的?

我尝试了这个,但是给了两个值:

I tried this but give two values instead:

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

推荐答案

您需要再次调用sum().示例-

You need to call sum() again. Example -

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

这篇关于如何对Pandas Data Frame中的所有数值求和以得出一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆