Skip to content
Related Articles

Related Articles

Improve Article

Python | Pandas Series.sum()

  • Last Updated : 10 Oct, 2018

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Series.sum() method is used to get the sum of the values for the requested axis.

Syntax: Series.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0)

axis : {index (0)}
skipna[boolean, default True] : Exclude NA/null values. If an entire row/column is NA, the result will be NA
level[int or level name, default None] : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar.
numeric_only[boolean, default None] : Include only float, int, boolean data. If None, will attempt to use everything, then use only numeric data

Returns: Returns the sum of the values for the requested axis

Code #1: By default, the sum of an empty or all-NA Series is 0.

# importing pandas module 
import pandas as pd 
# min_count = 0 is the default
# When passed  min_count = 1,
# sum of an empty series will be NaN
pd.Series([]).sum(min_count = 1)



Code #2:

# importing pandas module 
import pandas as pd 
# making data frame csv at url  
# sum of all salary
val = data['Salary'].sum()




Code #3:

# importing pandas module 
import pandas as pd 
# making a dict of list
data = {'name': ['John', 'Peter', 'Karl'],
        'age' : [23, 42, 19]}
val = pd.DataFrame(data)
# sum of all salary
val['total'] = val['age'].sum()


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :