4.5 C
London
Thursday, December 19, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Calculate the Sum of Columns in Pandas

How to Calculate the Sum of Columns in Pandas

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Often you may be interested in calculating the sum of one or more columns in a pandas DataFrame. Fortunately you can do this easily in pandas using the sum() function.

This tutorial shows several examples of how to use this function.

Example 1: Find the Sum of a Single Column

Suppose we have the following pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame({'rating': [90, 85, 82, 88, 94, 90, 76, 75, 87, 86],
                   'points': [25, 20, 14, 16, 27, 20, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 8, 5, 7, 6, 9, 9, 5],
                   'rebounds': [np.nan, 8, 10, 6, 6, 9, 6, 10, 10, 7]})

#view DataFrame 
df

	rating	points	assists	rebounds
0	90	25	5	NaN
1	85	20	7	8
2	82	14	7	10
3	88	16	8	6
4	94	27	5	6
5	90	20	7	9
6	76	12	6	6
7	75	15	9	10
8	87	14	9	10
9	86	19	5	7

We can find the sum of the column titled “points” by using the following syntax:

df['points'].sum()

182

The sum() function will also exclude NA’s by default. For example, if we find the sum of the “rebounds” column, the first value of “NaN” will simply be excluded from the calculation:

df['rebounds'].sum()

72.0

Example 2: Find the Sum of Multiple Columns

We can find the sum of multiple columns by using the following syntax:

#find sum of points and rebounds columns
df[['rebounds', 'points']].sum()

rebounds     72.0
points      182.0
dtype: float64

Example 3: Find the Sum of All Columns

We can find also find the sum of all columns by using the following syntax:

#find sum of all columns in DataFrame
df.sum()

rating      853.0
points      182.0
assists      68.0
rebounds     72.0
dtype: float64

For columns that are not numeric, the sum() function will simply not calculate the sum of those columns.

You can find the complete documentation for the sum() function here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories