Pandas: sum DataFrame rows for given columns(Pandas:对给定列求和 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
我想添加一列 '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
这篇关于Pandas:对给定列求和 DataFrame 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Pandas:对给定列求和 DataFrame 行


基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01