Python 来自熊猫数据帧的几列的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40116219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:10:47  来源:igfitidea点击:

Sum of several columns from a pandas dataframe

pythonpandasdataframe

提问by Pauline

So say I have the following table:

所以说我有下表:

In [2]: df = pd.DataFrame({'a': [1,2,3], 'b':[2,4,6], 'c':[1,1,1]})

In [3]: df
Out[3]: 
   a  b  c
0  1  2  1
1  2  4  1
2  3  6  1

I can sum a and b that way:

我可以这样对 a 和 b 求和:

In [4]: sum(df['a']) + sum(df['b'])
Out[4]: 18

However this is not very convenient for larger dataframe, where you have to sum multiple columns together.

但是,这对于较大的数据框来说不是很方便,因为您必须将多列相加。

Is there a neater way to sum columns (similar to the below)? What if I want to sum the entire DataFrame without specifying the columns?

有没有更简洁的方法来汇总列(类似于下面的)?如果我想在不指定列的情况下对整个 DataFrame 求和怎么办?

In [4]: sum(df[['a', 'b']]) #that will not work!
Out[4]: 18
In [4]: sum(df) #that will not work!
Out[4]: 21

回答by jezrael

I think you can use double sum- first DataFrame.sumcreate Seriesof sums and second Series.sumget sum of Series:

我认为您可以使用双重sum- 先DataFrame.sum创建Series总和,然后再Series.sum获取总和Series

print (df[['a','b']].sum())
a     6
b    12
dtype: int64

print (df[['a','b']].sum().sum())
18

You can also use:

您还可以使用:

print (df[['a','b']].sum(axis=1))
0    3
1    6
2    9
dtype: int64

print (df[['a','b']].sum(axis=1).sum())
18

Thank you pirSquaredfor another solution - convert dfto numpy arrayby valuesand then sum:

感谢pirSquared提供另一种解决方案 - 转换dfnumpy arraybyvalues然后sum

print (df[['a','b']].values.sum())
18


print (df.sum().sum())
21

回答by Fermin Pitol

Maybe you are looking something like this:

也许你正在寻找这样的东西:

df["result"] = df.apply(lambda row: row['a' : 'c'].sum(),axis=1)