I’m trying to add the "vertical" and "horizontal" sum for each cell.
So far I’ve tired to get the sum of each row and column. But like this I’d have to iterate over every value and add them which seems quite inefficient.
You could combine sum() and sum(1) to create a new dataframe but then the cell in question is counted twice.
Used df :
df = pd.DataFrame([[1,3], [8,4]], columns=list("ab"))
Is there any simple operation to archive the wanted result?
>Solution :
IIUC use numpy broadcasting and subtract original DataFrame:
out = df.sum().to_numpy() + df.sum(1).to_numpy()[:, None] - df
print (out)
a b
0 12 8
1 13 15
