TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas sum()

Pandas sum() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

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:

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

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:

import pandas as pd    
# default min_count = 0  
pd.Series([]).sum() 
# Passed min_count = 1, then sum of an empty series will be NaN 
pd.Series([]).sum(min_count = 1)

Output

0.0
nan 

Example2:

import pandas as pd  
# making a dict of list 
info = {'Name': ['Parker', 'Smith', 'William'], 
        'age' : [32, 28, 39]}   
data = pd.DataFrame(info)   
# sum of all salary stored in 'total'
data['total'] = data['age'].sum()   
print(data)

Output

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





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf