7

I have a df as such:

         column A    column B    column C  .... ColumnZ 
 index
   X        1           4           7              10
   Y        2           5           8              11
   Z        3           6           9              12

For the life on me I can't figure out how to sum rows for each column, to arrive at a summation df:

         column A    column B    column C  .... ColumnZ 
 index
 total       6           16          25             33

Any thoughts?

Steel
  • 507
  • 4
  • 13

2 Answers2

10

You can use:

df.loc['total'] = df.sum(numeric_only=True, axis=0)
Jonas
  • 1,749
  • 1
  • 10
  • 18
5

Try this:

import pandas as pd

df = pd.DataFrame({'column A': [1, 2, 3], 'column B': [4, 5, 6], 'column C': [7, 8, 9]})

df.loc['total'] = df.sum()

print(df)

Output:

       column A  column B  column C
0             1         4         7
1             2         5         8
2             3         6         9
total         6        15        24
Red
  • 26,798
  • 7
  • 36
  • 58