In this article we will discuss how to get the sum column values in a pandas dataframe. We will cover the following topics in detail,
 Get the sum of all column values in a dataframe
 Select the column by name and get the sum of all values in that column
 Select the column by position and get the sum of all values in that column
 Get the sum of columns values for selected rows only in Dataframe
 Get the sum of column values in a dataframe based on condition
First of all, we will create a dataframe from list of tuples,
import pandas as pd import numpy as np # List of Tuples students = [('jack', 34, 'Sydney', 155), ('Riti', 31, 'Delhi', 177.5), ('Aadi', 16, 'Mumbai', 81), ('Mohit', 31, 'Delhi', np.NaN), ('Veena', np.NaN, 'Delhi', 144), ('Shaunak', 35, 'Mumbai', 135), ('Shaun', 35, 'Colombo', 111) ] # Create a DataFrame object df = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score']) print(df)
Output:
Name Age City Score 0 jack 34.0 Sydney 155.0 1 Riti 31.0 Delhi 177.5 2 Aadi 16.0 Mumbai 81.0 3 Mohit 31.0 Delhi NaN 4 Veena NaN Delhi 144.0 5 Shaunak 35.0 Mumbai 135.0 6 Shaun 35.0 Colombo 111.0
This dataframe contains information about students like their name, age, city and score.
Now letâ€™s see how to get the sum of values in the column â€˜Scoreâ€™ of this dataframe.
Get the sum of column values in a dataframe
Select the column by name and get the sum of all values in that column
Select a column from a dataframe by the column name and the get the sum of values in that column using the sum() function,
# Get total all values in column 'Score' of the DataFrame total = df['Score'].sum() print(total)
Output:
803.5
Here we selected the column â€˜Scoreâ€™ from the dataframe using [] operator and got all the values as Pandas Series object. Then we called the sum() function on that Series object to get the sum of values in it. So, it gave us the sum of values in the column â€˜Scoreâ€™ of the dataframe.
We can also select the column using loc[] and then we can get the sum of values in that column. For examples,
# Select column 'Score' using loc[] and calculate sum of all # values in that column total = df.loc[:, 'Score'].sum() print(total)
Output:
803.5
Here we selected the column â€˜Scoreâ€™ as Series object using loc[] and then we called the sum() function on the Series object to get the sum of all values in the column â€˜Scoreâ€™ of the dataframe.
Know more about: Selecting columns by name from the dataframe using the loc[]
Select the column by position and get the sum of all values in that column
Suppose we donâ€™t have the column name but we know the position of a column in dataframe and we want the sum of values in that column. For that we will select the column by number or position in the dataframe using iloc[] and it will return us the column contents as a Series object. Then we will call the sum() function on that series,
# Get sum of all values in 4th column column_number = 4 total = df.iloc[:, column_number1:column_number].sum() print(total)
Output:
Score 803.5 dtype: float64
It returned a Series with single value.
Here we selected the 4th column from the dataframe as a Series object using the iloc[] and the called the sum() function on the series object. So, it returned the sum of values in the 4th column i.e. column â€˜Scoreâ€™.
Know more about: Selecting columns by the number from dataframe using the iloc[]
Get the sum of columns values for selected rows only in Dataframe
Select a column from Dataframe and get the sum of specific entries in that column. For example,
# Select 4th column of dataframe and get sum of first 3 values in that column total = df.iloc[0:3, 3:4].sum() print(total)
Output:
Score 413.5 dtype: float64
It returned a Series with single value.
Here we selected the first 3 rows of the 3rd column of the dataframe and then calculated its sum.
Get the sum of column values in a dataframe based on condition
Suppose in the above dataframe we want to get the sum of the score of students from Delhi only. For that we need to select only those values from the column â€˜Scoreâ€™ where â€˜Cityâ€™ is Delhi. Letâ€™s see how to do that,
# Get sum of values in a column 'Score' # for those rows only where 'City' is 'Delhi' total = df.loc[df['City'] == 'Delhi', 'Score'].sum() print(total)
Output:
321.5
Using loc[] we selected the column â€˜Scoreâ€™ but for only those rows where column â€˜Cityâ€™ has value â€˜Delhiâ€™. Then we called the sum() function on the series object to get the sum of scores of students from â€˜Delhiâ€™. So, basically we selected rows from a dataframe that satisfy our condition and then selected the values of column â€˜Scoreâ€™ for those rows only. We did that in a single expression using loc[].
Know more about:Â loc[] & iloc[]
Conclusion:
These were the different ways to get the sum of all or specific values in a dataframe column in Pandas.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.