How to 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, I’ll demonstrate how to apply the above syntax using a simple example.

Steps to Sum each Column and Row in Pandas DataFrame

Step 1: Prepare your Data

To start with an example, suppose that you prepared the following data about the commission earned by your 3 employees (over the first 6 months of the year):

dataset

Your goal is to sum all the commissions earned:

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

Step 2: Create the DataFrame

Next, you’ll need to create the DataFrame in order to capture the above data in Python:

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Bill Commission': [1500,2200,3500,1800,3000,2800],
        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
        'Jack Commission': [1700,3100,3300,2700,2400,3100]
        }

df = DataFrame(Data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
print (df)

This is how the DataFrame would look like in Python:

DataFrame in Python

Step 3: Sum each Column and Row in Pandas DataFrame

In order to sum each column in the DataFrame, you can use the syntax that was introduced at the beginning of this guide:

df.sum(axis=0) 

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

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Bill Commission': [1500,2200,3500,1800,3000,2800],
        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
        'Jack Commission': [1700,3100,3300,2700,2400,3100]
        }

df = DataFrame(Data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
sum_column = df.sum(axis=0)
print (sum_column) 

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

How to Sum each Column and Row in Pandas DataFrame

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

df.sum(axis=1)

For our example, you may run this code in Python:

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Bill Commission': [1500,2200,3500,1800,3000,2800],
        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
        'Jack Commission': [1700,3100,3300,2700,2400,3100]
        }

df = DataFrame(Data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
sum_row = df.sum(axis=1)
print (sum_row) 

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

Sum each Column and Row in Pandas DataFrame

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

In the next section, I’ll show you 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:

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Bill Commission': [1500,2200,3500,1800,3000,2800],
        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
        'Jack Commission': [1700,3100,3300,2700,2400,3100]
        }

df = DataFrame(Data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
count_column = df.count(axis=0)
print (count_column) 

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

Count each Column and Row in Pandas DataFrame

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:

from pandas import DataFrame

Data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
        'Bill Commission': [1500,2200,3500,1800,3000,2800],
        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
        'Jack Commission': [1700,3100,3300,2700,2400,3100]
        }

df = DataFrame(Data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
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:

How to Count each Column and Row in Pandas DataFrame