pandas 除一个外,所有行总和与熊猫
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44003371/
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
All row sum with pandas except one
提问by Glori P.
I have several tables on a PostgreSQL database that look more or less like that:
我在 PostgreSQL 数据库上有几个表,看起来或多或少是这样的:
gid col2 col1 col3
6 15 45 77
1 15 45 57
2 14 0.2 42
3 12 6 37
4 9 85 27
5 5 1 15
For each table, numbers and columns' names change (I created them in a loop in python).
对于每个表,数字和列的名称都会发生变化(我在 python 中循环创建了它们)。
I would like to have back another column called sum for each table with the sum of each calumn except for the gid. The goal is having something like that:
我想为每个表返回另一个名为 sum 的列,其中包含除 gid 之外的每个 calumn 的总和。目标是有这样的东西:
gid col2 col1 col3 sum
6 15 45 77 137
1 15 45 57 117
2 14 0.2 42 56.2
3 12 6 37 55
4 9 85 27 121
5 5 1 15 21
I cannot use column name: the only one with no changes is gid
.
我不能使用列名:唯一没有改变的是gid
.
Some idea to make it with python
(pandas
, numpy
) or psql
?
用python
( pandas
, numpy
) 或psql
?
回答by jezrael
df['sum'] = df.drop('gid', axis=1).sum(axis=1)
print (df)
gid col2 col1 col3 sum
0 6 15 45.0 77 137.0
1 1 15 45.0 57 117.0
2 2 14 0.2 42 56.2
3 3 12 6.0 37 55.0
4 4 9 85.0 27 121.0
5 5 5 1.0 15 21.0
If gid
is always first column, select by iloc
all columns without first and then sum
them:
如果gid
始终是第一列,则按iloc
所有列选择,而不是先选择,然后选择sum
它们:
df['sum'] = df.iloc[:, 1:].sum(axis=1)
print (df)
gid col2 col1 col3 sum
0 6 15 45.0 77 137.0
1 1 15 45.0 57 117.0
2 2 14 0.2 42 56.2
3 3 12 6.0 37 55.0
4 4 9 85.0 27 121.0
5 5 5 1.0 15 21.0