[英]Sum of only specific columns in a pandas dataframe
Consider I have a dataframe with few columns考虑我有一个 dataframe 有几列
and a list ['salary','gross exp']和一个列表 ['salary','gros exp']
Now I want to perform sum of the column operation only on the columns from the list on the dataframe and save that to the dataframe现在我想仅对 dataframe 列表中的列执行列求和并将其保存到 dataframe
To put this in prespective, the list of columns ['salary','gross exp'] are money related and it makes sense to perform sum on these columns and not touch any of the other columns对此,列 ['salary','gross exp'] 的列表与金钱相关,对这些列执行求和而不触及任何其他列是有意义的
PS: I have several Excel workbooks to work on and each consists of few tens of sheets, so doing it manually is out of options PS:我有几个 Excel 工作簿要处理,每个工作簿都包含几十张,所以手动操作是不可能的
Also macro code for excel works fine if that's possible如果可能的话,excel 的宏代码也可以正常工作
TIA TIA
Working with the following example:使用以下示例:
import pandas as pd
list_ = ['sallary', 'gross exp']
d = {'sallary': [1,2,3], 'gross exp': [2,2,2], 'another column': [10,10,10]}
df = pd.DataFrame(d)
df
sallary![]() |
gross exp![]() |
another column![]() |
|
---|---|---|---|
0 ![]() |
1 ![]() |
2 ![]() |
10 ![]() |
1 ![]() |
2 ![]() |
2 ![]() |
10 ![]() |
2 ![]() |
3 ![]() |
2 ![]() |
10 ![]() |
You can then add a new empty row, and insert the sum of the columns that you have from the list in that same row:然后,您可以添加一个新的空行,并在同一行中插入您从列表中获得的列的总和:
df = df.append(pd.Series(dtype = float), ignore_index=True)
df.loc[df.index[-1],list_] = df[list_].sum()
df
sallary![]() |
gross exp![]() |
another column![]() |
|
---|---|---|---|
0 ![]() |
1.0 ![]() |
2.0 ![]() |
10.0 ![]() |
1 ![]() |
2.0 ![]() |
2.0 ![]() |
10.0 ![]() |
2 ![]() |
3.0 ![]() |
2.0 ![]() |
10.0 ![]() |
3 ![]() |
6.0 ![]() |
6.0 ![]() |
NaN![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.