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 your Data
To start with an example, suppose that you prepared the following data about the commission earned by 3 of your employees (over the first 6 months of the year):
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, create the DataFrame in order to capture the above data in Python:
import pandas as pd 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 = pd.DataFrame(data,columns=['Month','Bill Commission','Maria Commission','Jack Commission']) print (df)
This is how the DataFrame would look like 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:
import pandas as pd 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 = pd.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:
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:
import pandas as pd 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 = pd.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:
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 '], '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 = pd.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:
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 '], '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 = pd.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:
You may also want to check the Pandas Documentation for additional information.