Sum each Column and Row in Pandas DataFrame

To sum each numeric column and row in Pandas DataFrame (make sure to apply it to numeric values):

(1) Sum each column:

df.sum(axis=0) 

(2) Sum each row:

df.sum(axis=1)

Steps to Sum each Column and Row in Pandas DataFrame

Step 1: Prepare the Data

For example, here is a simple dataset that contains information about the commission earned by 3 people (over the first 6 months of the year):

montha_commission b_commission c_commission 
Jan150032001700
Feb220041003100
Mar350025003300
Apr180030002700
May300047002400
Jun280034003100

The goal is to sum the commissions earned:

  • For each person over the 6 months (sum by column)
  • For each month across all people (sum by row)

Step 2: Sum each Column and Row in Pandas DataFrame

Use df.sum(axis=0) in order to sum each numeric column in the DataFrame:

import pandas as pd

data = {"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
        "a_commission": [1500, 2200, 3500, 1800, 3000, 2800],
        "b_commission": [3200, 4100, 2500, 3000, 4700, 3400], 
        "c_commission": [1700, 3100, 3300, 2700, 2400, 3100]
        }

df = pd.DataFrame(data, columns=["a_commission", "b_commission", "c_commission"])

df_sum = df.sum(axis=0)

print(df_sum)

The total commission earned by each person over the first 6 months:

a_commission                       14800
b_commission                       20900
c_commission                       16300

Alternatively, you can sum each numeric row in your DataFrame using df.sum(axis=1):

import pandas as pd

data = {"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
        "a_commission": [1500, 2200, 3500, 1800, 3000, 2800],
        "b_commission": [3200, 4100, 2500, 3000, 4700, 3400], 
        "c_commission": [1700, 3100, 3300, 2700, 2400, 3100]
        }

df = pd.DataFrame(data, columns=["a_commission", "b_commission", "c_commission"])

df_sum = df.sum(axis=1)

print(df_sum)

The total commission earned in each month across all people:

0     6400
1     9400
2     9300
3     7500
4    10100
5     9300

Count for each Column and Row in Pandas DataFrame

To get the count for each column in the DataFrame using df.count(axis=0):

import pandas as pd

data = {"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
        "a_commission": [1500, 2200, 3500, 1800, 3000, 2800],
        "b_commission": [3200, 4100, 2500, 3000, 4700, 3400], 
        "c_commission": [1700, 3100, 3300, 2700, 2400, 3100]
        }

df = pd.DataFrame(data)

df_count = df.count(axis=0)

print(df_count)

The count is 6 for each column in the DataFrame:

month           6
a_commission    6
b_commission    6
c_commission    6

To get the count for each row in the DataFrame using df.count(axis=1):

import pandas as pd

data = {"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
        "a_commission": [1500, 2200, 3500, 1800, 3000, 2800],
        "b_commission": [3200, 4100, 2500, 3000, 4700, 3400], 
        "c_commission": [1700, 3100, 3300, 2700, 2400, 3100]
        }

df = pd.DataFrame(data)

df_count = df.count(axis=1)

print(df_count)

The count is 4 for each row in the DataFrame:

0    4
1    4
2    4
3    4
4    4
5    4

You may check the following guide for the steps to average each column and row in Pandas DataFrame.

Additional information is also available in the Pandas Documentation.

Leave a Comment