Pandas:对给定列的DataFrame行求和

时间:2023-02-07 15:25:55

I have the following DataFrame:

我有以下DataFrame:

In [1]:

import pandas as pd
df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd':[5,9,1]})
df
Out [1]:
   a  b   c  d
0  1  2  dd  5
1  2  3  ee  9
2  3  4  ff  1

I would like to add a column 'e' which is the sum of column 'a', 'b' and 'd'.

我想添加一个列'e',它是'a','b'和'd'列的总和。

Going across forums, I thought something like this would work:

穿过论坛,我觉得这样的事情会起作用:

df['e'] = df[['a','b','d']].map(sum)

But no!

但不是!

I would like to realize the operation having the list of columns ['a','b','d'] and df as inputs.

我想实现具有列列表['a','b','d']和df作为输入的操作。

4 个解决方案

#1


118  

You can just sum and set param axis=1 to sum the rows, this will ignore none numeric columns:

你可以求和并设置param axis = 1来对行进行求和,这将忽略无数字列:

In [91]:

df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd':[5,9,1]})
df['e'] = df.sum(axis=1)
df
Out[91]:
   a  b   c  d   e
0  1  2  dd  5   8
1  2  3  ee  9  14
2  3  4  ff  1   8

If you want to just sum specific columns then you can create a list of the columns and remove the ones you are not interested in:

如果您只想对特定列求和,那么您可以创建列的列表并删除您不感兴趣的列:

In [98]:

col_list= list(df)
col_list.remove('d')
col_list
Out[98]:
['a', 'b', 'c']
In [99]:

df['e'] = df[col_list].sum(axis=1)
df
Out[99]:
   a  b   c  d  e
0  1  2  dd  5  3
1  2  3  ee  9  5
2  3  4  ff  1  7

#2


18  

If you have just a few columns to sum, you can write:

如果你只有几列要总和,你可以写:

df['e'] = df['a'] + df['b'] + df['d']

This creates new column e with the values:

这将创建具有以下值的新列e:

   a  b   c  d   e
0  1  2  dd  5   8
1  2  3  ee  9  14
2  3  4  ff  1   8

For longer lists of columns, EdChum's answer is preferred.

对于较长的列列表,EdChum的答案是首选。

#3


2  

This is a simpler way using iloc to select which columns to sum:

这是一种使用iloc选择要汇总的列的更简单方法:

df['f']=df.iloc[:,0:2].sum(axis=1)
df['g']=df.iloc[:,[0,1]].sum(axis=1)
df['h']=df.iloc[:,[0,3]].sum(axis=1)

Produces:

生产:

   a  b   c  d   e  f  g   h
0  1  2  dd  5   8  3  3   6
1  2  3  ee  9  14  5  5  11
2  3  4  ff  1   8  7  7   4

I can't find a way to combine a range and specific columns that works e.g. something like:

我找不到一种方法来组合范围和特定列,例如就像是:

df['i']=df.iloc[:,[[0:2],3]].sum(axis=1)
df['i']=df.iloc[:,[0:2,3]].sum(axis=1)

#4


0  

Create a list of column names you want to add up.

创建要添加的列名列表。

df['total']=df.loc[:,list_name].sum(axis=1)

If you want the sum for certain rows, specify the rows using ':'

如果您想要某些行的总和,请使用“:”指定行

#1


118  

You can just sum and set param axis=1 to sum the rows, this will ignore none numeric columns:

你可以求和并设置param axis = 1来对行进行求和,这将忽略无数字列:

In [91]:

df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd':[5,9,1]})
df['e'] = df.sum(axis=1)
df
Out[91]:
   a  b   c  d   e
0  1  2  dd  5   8
1  2  3  ee  9  14
2  3  4  ff  1   8

If you want to just sum specific columns then you can create a list of the columns and remove the ones you are not interested in:

如果您只想对特定列求和,那么您可以创建列的列表并删除您不感兴趣的列:

In [98]:

col_list= list(df)
col_list.remove('d')
col_list
Out[98]:
['a', 'b', 'c']
In [99]:

df['e'] = df[col_list].sum(axis=1)
df
Out[99]:
   a  b   c  d  e
0  1  2  dd  5  3
1  2  3  ee  9  5
2  3  4  ff  1  7

#2


18  

If you have just a few columns to sum, you can write:

如果你只有几列要总和,你可以写:

df['e'] = df['a'] + df['b'] + df['d']

This creates new column e with the values:

这将创建具有以下值的新列e:

   a  b   c  d   e
0  1  2  dd  5   8
1  2  3  ee  9  14
2  3  4  ff  1   8

For longer lists of columns, EdChum's answer is preferred.

对于较长的列列表,EdChum的答案是首选。

#3


2  

This is a simpler way using iloc to select which columns to sum:

这是一种使用iloc选择要汇总的列的更简单方法:

df['f']=df.iloc[:,0:2].sum(axis=1)
df['g']=df.iloc[:,[0,1]].sum(axis=1)
df['h']=df.iloc[:,[0,3]].sum(axis=1)

Produces:

生产:

   a  b   c  d   e  f  g   h
0  1  2  dd  5   8  3  3   6
1  2  3  ee  9  14  5  5  11
2  3  4  ff  1   8  7  7   4

I can't find a way to combine a range and specific columns that works e.g. something like:

我找不到一种方法来组合范围和特定列,例如就像是:

df['i']=df.iloc[:,[[0:2],3]].sum(axis=1)
df['i']=df.iloc[:,[0:2,3]].sum(axis=1)

#4


0  

Create a list of column names you want to add up.

创建要添加的列名列表。

df['total']=df.loc[:,list_name].sum(axis=1)

If you want the sum for certain rows, specify the rows using ':'

如果您想要某些行的总和,请使用“:”指定行