简体   繁体   English

来自pandas数据帧的几列的总和

[英]Sum of several columns from a pandas dataframe

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

I think you can use double sum - first DataFrame.sum create Series of sums and second Series.sum get 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 pirSquared for another solution - convert df to numpy array by values and then sum : 谢谢pirSquared的另一个解决方案 - 通过valuesdf转换为numpy array然后sum

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

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

也许你看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM