Home » Pandas sum()

Pandas DataFrame.sum()

Pandas DataFrame.sum() function is used to return the sum of the values for the requested axis by the user. If the input value is an index axis, then it will add all the values in a column and works same for all the columns. It returns a series that contains the sum of all the values in each column.

It is also capable of skipping the missing values in the DataFrame while calculating the sum in the DataFrame.

Syntax:

Parameters

  • axis: {index (0), columns (1)}

0 or ‘index’ is used for row-wise, whereas 1 or ‘columns’ is used for column-wise.

  • skipna: bool, default True

It is used to exclude all the null values.

  • level: int or level name, default None

It counts along a particular level and collapsing into a series, if the axis is a multiindex.

  • numeric_only: bool, default value None

It includes only int, float, and boolean columns. If it is None, it will attempt to use everything, so numeric data should be used.

  • min_count: int, default value 0

It refers to the required number of valid values to perform any operation. If it is fewer than the min_count non-NA values are present, then the result will be NaN.

  • **kwargs: It is an optional parameter that is to be passed to a function.

Returns:

It returns the sum of Series or DataFrame if a level is specified.

Example1:

Output

0.0  nan   

Example2:

Output

     Name     age   total  0   Parker    32     99  1   Smith     28     99  2   William   39     99  

You may also like