Sum each Column and Row in Pandas DataFrame

You may use the following syntax to sum each column and row in Pandas DataFrame:

(1) Sum each column:

df.sum(axis=0) 

(2) Sum each row:

df.sum(axis=1)

In the next section, you’ll see how to apply the above syntax using a simple example.

Steps to Sum each Column and Row in Pandas DataFrame

Step 1: Prepare the Data

To start with an example, let’s suppose that you have the following data about the commission earned by 3 people (over the first 6 months of the year):

month a_commission  b_commission  c_commission 
Jan 1500 3200 1700
Feb 2200 4100 3100
Mar 3500 2500 3300
Apr 1800 3000 2700
May 3000 4700 2400
Jun 2800 3400 3100

Your goal is to sum all the commissions earned:

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

Step 2: Create a DataFrame

Next, create a DataFrame in order to capture the above data in Python:

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)
print (df)

This is how the DataFrame would look like in Python:

  month  a_commission  b_commission  c_commission
0  Jan           1500          3200          1700
1  Feb           2200          4100          3100
2  Mar           3500          2500          3300
3  Apr           1800          3000          2700
4  May           3000          4700          2400
5  Jun           2800          3400          3100

Step 3: Sum each Column and Row in Pandas DataFrame

In order to sum each column in the DataFrame, you may use the following syntax:

df.sum(axis=0) 

In the context of our example, you can apply this code to sum each column:

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)
sum_column = df.sum(axis=0)
print (sum_column)

Run the code in Python, and you’ll get the total commission earned by each person over the 6 months:

a_commission                       14800
b_commission                       20900
c_commission                       16300

Alternatively, you can sum each row in your DataFrame using this syntax:

df.sum(axis=1)

For our example:

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)
sum_row = df.sum(axis=1)
print (sum_row)

You’ll then get the total commission earned in each month across all people:

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

What if you want to get the count, rather than the sum, for each column and row in your DataFrame?

In the next section, you’ll see how to perform this task.

Count for each Column and Row in Pandas DataFrame

You can use the following syntax to get the count of values for each column:

df.count(axis=0)

For our example, run this code to get the count:

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)
count_column = df.count(axis=0)
print (count_column)

You’ll then see the count of 6 for each column in the DataFrame:

month           6
a_commission    6
b_commission    6
c_commission    6

And if you want to get the count for each row, you can then use this syntax:

df.count(axis=1)

So in our case, the code would look like this:

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)
count_row = df.count(axis=1)
print (count_row)

Once you run the code, you’ll get the count of 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.