Pandas:对给定列求和 DataFrame 行

2022-01-09 00:00:00 python pandas dataframe sum

问题描述

我有以下数据框:

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

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

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

浏览论坛,我认为这样的事情会起作用:

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

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

但它没有.

我想知道以 ['a','b','d']df 列的列表作为输入的适当操作.

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


解决方案

你可以只 sum 并设置参数 axis=1 对行求和,这将忽略 none数字列:

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

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

相关文章