Skip to main content

Pandas dataframe.sum() function return the sum of the values for the requested axis. If the input is index axis then it adds all the values in a column and repeats the same for all the columns and returns a series containing the sum of all the values in each column. It also provides support to skip the missing values in the dataframe while calculating the sum in the dataframe. The sum function helps in finding the sum of the values for desired axis.

Syntax

pandas.DataFrame.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, kwargs)

  • axis : {index (0), columns (1)} – This is the axis where the function is applied.
  • skipna : bool, default True – It is used to decide whether the NA/Null values should be dropped/skipped or not while computation.
  • level : int or level name, default None – It used for deciding the level, generally in case of multindex dataframes.
  • numeric_only : bool,default None – It used to decide whether to include only float, int, boolean columns. If None, will attempt to use everything
  • min_count : int,default 0 – The required number of valid values to perform the operation.
  • kwargs : Additional Arguments.

Example 1: Using sum function with multindex dataframe

Input:

df_sum = pd.MultiIndex.from_arrays([
     ['Sedan', 'Hatchback', 'Sedan', 'Hatchback'],
    ['BMW', 'Mini Cooper', 'Audi', 'Aston Martin']],
  names=['designs', 'companies'])

Input:

cars = pd.Series([3, 6, 9, 18], name='types_of_Cars', index=df_sum)

Input:

cars.sum()

Output:

36

Example 2: Using sum function with level parameter

Here since we have multindex dataframe, therefore we can perform sum function using level parameter. In this example, we can see how the levels are used in sum() function of pandas.

Input:

cars.sum(level='designs')

Output:

designs
Sedan        12
Hatchback    24
Name: types_of_Cars, dtype: int64

Input:

cars.sum(level=0)

Output:

designs
Sedan        12
Hatchback    24
Name: types_of_Cars, dtype: int64

Input:

cars.sum(level=1)

Output:

companies
BMW              3
Mini Cooper      6
Audi             9
Aston Martin    18
Name: types_of_Cars, dtype: int64

Example 3:  Use sum() function to find the sum of all the values over the column axis.

Now we will find the sum along the column axis. We are going to set skipna to be true. If we do not skip the NaN values then it will result in NaN values.

input:

# importing pandas as pd 
import pandas as pd 
  
# Creating the dataframe  
df = pd.read_csv("nba.csv") 
  
# sum over the column axis. 
df.sum(axis = 1, skipna = True) 

output:

Tags
Submitted by shiksha.dahiya on February 16, 2021

Shiksha is working as a Data Scientist at iVagus. She has expertise in Data Science and Machine Learning.

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.