I have a dataframe as the following:
import pandas as pd
df = pd.DataFrame([(1,2,3,4,5,6),
(1,2,3,4,5,6),
(1,2,3,4,5,6)], columns=['a','b','c','d','e','f'])
Out:
a b c d e f
0 1 2 3 4 5 6
1 1 2 3 4 5 6
2 1 2 3 4 5 6
and I want to get the sum of all the elements of the dataframe, always excluding one column. In this example the desired outcome would be:
60 57 54 51 48 45
I have found a solution that seems to do the job, but I'm pretty sure there must be a more efficient way to do the same:
for x in df.columns:
df.drop(columns = x).sum().sum()
Use DataFrame.rsub
for subtract from right side summed rows by df.sum(axis=1)
, last add sum
for sum per columns:
s = df.rsub(df.sum(axis=1), axis=0).sum()
print (s)
a 60
b 57
c 54
d 51
e 48
f 45
dtype: int64
Simply subtract the total sum with the sum per column. This is efficiently done using an assignment expression :
out = (s:=df.sum()).sum()-s
output:
a 60
b 57
c 54
d 51
e 48
f 45
Yet another way: Subtract the column sums from the total sum of the DataFrame using broadcasting:
out = df.sum().sum() - df.sum()
Output:
a 60
b 57
c 54
d 51
e 48
f 45
dtype: int64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.