我有以下数据框:
In [1]:
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 columns 'a'
, 'b'
and 'd'
.
跨过论坛,我认为这样的事情会起作用:
df['e'] = df[['a', 'b', 'd']].map(sum)
但它没有。
我想知道以列列表 ['a', 'b', 'd']
和 df
作为输入的适当操作。
原文由 Colonel Beauvel 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以只
sum
并设置参数axis=1
对行求和,这将忽略非数字列:如果您只想对特定列求和,则可以创建列列表并删除您不感兴趣的列: