简体   繁体   English

除Python列表中指定的列外的所有列的总和

[英]Sum of all columns except the ones specified in a list in Python

I have the below data for each ID: 每个ID我都有以下数据:

id  ----    Base    AE      Val     LT  RO+ Prem    AM  TN  T3  AR
05  0       34.34   9.42    70.68   0   0   0   0   0   0   0
108 0       43.77   0       28      0   0   0   0   0   0   0
205 0       77.64   0       32.2    0   0   0   0   0   0   0
320 0       66.24   0       59.628  0   0   0   0   0   0   0
313 0       21.66   0       21.442  0   0   0   0   0   0   0
324 0       72.37   0       701.12  0   0   0   0   0   0   0
505 0       76.057  0       43.87   0   0   0   0   0   0   0

Now I want to find the sum of all columns except a few which I specify and the others separately into a column like below: 现在,我想找到所有列的总和,除了我指定的几列和其他列分开的列,如下所示:

id  Base    Val     Others  Total
05  34.34   70.68   9.42    114.441387
108 43.77   28      0       71.77
205 77.64   32.2    0       109.84
320 66.24   59.628  0       125.868
313 21.66   21.442  0       43.102
324 72.37   701.12  0       773.49
505 76.057  43.87   0       119.927

So if my list of columns to keep: 因此,如果我要保留的列列表:

cols_to_keep = ['Base','Val']

The other channels which are not part of this list,are to be summed up in Others Column and all the values in each row sum to Total. 不属于此列表的其他通道将在“其他”列中汇总,并且每行中的所有值总计为总计。 id is the index of the records. id是记录的索引。

I am able to keep the columns I declare in the list, but how to sum up the other columns except in the list in the Others column. 我能够将我声明的列保留在列表中,但是如何总结除“其他”列中的列表以外的其他列。 Can someone please help me with this? 有人可以帮我吗? The data is in a pandas df. 数据在pandas df中。

Drop the columns you do not wish to sum over: 删除不希望求和的列:

df['Others'] = df.drop(cols_to_keep, axis=1).sum(axis=1)
df['Total'] = df.sum(axis=1)

Use assign , for filter columns use Index.difference : 使用assign ,对于过滤器列,使用Index.difference

cols_to_keep = ['Base','Val'] 

c = df.columns.difference(cols_to_keep)
df = df[cols_to_keep].assign(Others=df[c].sum(axis=1), Total=df.sum(1))
print (df)
       Base      Val  Others    Total
id                                   
5    34.340   70.680    9.42  114.440
108  43.770   28.000    0.00   71.770
205  77.640   32.200    0.00  109.840
320  66.240   59.628    0.00  125.868
313  21.660   21.442    0.00   43.102
324  72.370  701.120    0.00  773.490
505  76.057   43.870    0.00  119.927
In [47]: !cat b.txt | tr -s ' ' > data.txt
    ...: df = pd.read_csv("data.txt",sep=" ", dtype={'id':str})
    ...: df['Others'] = df['AE']
    ...: df['Total']  = df['Base'] + df['Others'] + df['Val']
    ...:
    ...: cols_to_keep=['id', 'Base', 'Val','Others','Total']
    ...: c = df.columns.difference(cols_to_keep)
    ...: df.drop(c, axis=1)
    ...: newDf = df.drop(c, axis=1)
    ...:

In [48]: newDf
Out[48]:
    id    Base      Val  Others    Total
0   05  34.340   70.680    9.42  114.440
1  108  43.770   28.000    0.00   71.770
2  205  77.640   32.200    0.00  109.840
3  320  66.240   59.628    0.00  125.868
4  313  21.660   21.442    0.00   43.102
5  324  72.370  701.120    0.00  773.490
6  505  76.057   43.870    0.00  119.927

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除python中“列表”中指定的文件和目录以外的所有文件和目录 - Deleting all files and directories except a few specified in a “list” in python 如何缩放除 pandas dataframe 中的某些列之外的所有列? - How to scale all columns except certain ones in pandas dataframe? 如何将除少数特定列之外的所有列转换为小写? - How to convert to lowercase all columns except a few specific ones? 如何删除DataFrame中除某些列外的所有列? - How to delete all columns in DataFrame except certain ones? 除 1 以外的所有列的 Spark groupby 总和 - Spark groupby sum for all columns except 1 对列表中所有元素求和,除了第一个 - sum all the elements in the list of lists except first 如何打印所有已定义的变量,除了等于0的变量 - Python3 - How to print all defined variables except for the ones that are equal to 0 - Python3 删除所有数字,除了使用 python regex 组合成字符串的数字 - Remove all numbers except for the ones combined to string using python regex python:如何获取数组中除条件以外的所有成员 - python: how to get all members of an array except for ones that match a condition 如何使用除第一个包含名称的列之外的所有列的 pandas 计算 python 中的累积总和? - How to calculate cumulative sum in python using pandas of all the columns except the first one that contain names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM