How to sum all rows of a dataframe with pandas ?

Published: August 31, 2021

Tags: Python; Pandas; DataFrame;

DMCA.com Protection Status

Examples of how to sum all all rows of a dataframe with pandas:

Create a dataframe

Let's first create a dataframe with pandas:

import pandas as pd
import numpy as np

data = np.random.randint(5, size=(3,3))

df = pd.DataFrame(data=data,columns=['A','B','C'])

gives for example

   A  B  C
0  0  2  0
1  2  3  0
2  3  1  2

Sum all rows of a dataframe with pandas

To sum all rows with pandas, a solution is to use

df_sum = df.sum(axis=0)

returns

A    5
B    6
C    2

Note that df_sum is a serie here:

print(type(df_sum))

gives

<class 'pandas.core.series.Series'>

Stored sum in a new row

To save result in a new row, a solution is to change df_sum from a serie to a dataframe:

df_sum = df.sum(axis=0).to_frame()

transpose it:

df_sum = df_sum.T

print(df_sum)

gives

   A  B  C
0  5  6  2

Note: we can also change the index name:

df_sum.index = ['Total']

returns

       A  B  C
Total  5  6  2

Now, we can append the sum result in our source dataframe:

df = pd.concat([df_sum,df], ignore_index=False)

gives

       A  B  C
Total  5  6  2
0      0  2  0
1      2  3  0
2      3  1  2

or if we want the total at this end row:

df = pd.concat([df,df_sum], ignore_index=False)

gives

       A  B  C
0      0  2  0
1      2  3  0
2      3  1  2
Total  5  6  2

How to skip first or last rows

Examples of how to skip first or last rows if necessary:

import pandas as pd
import numpy as np

data = np.random.randint(5, size=(3,3))

df = pd.DataFrame(data=data,columns=['A','B','C'])

returns

   A  B  C
0  0  4  2
1  4  1  0
2  2  0  1

Skip first row:

df.iloc[1:].sum(axis=0)

returns

A    6
B    1
C    1

Skip last row:

df.iloc[:-1].sum(axis=0)

returns

    A    4
B    5
C    2
dtype: int64

References