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

### 问题描述

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

## 推荐答案

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

## 其他推荐答案

:

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

```224
```

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

### 问题描述

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)
```