C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
0 or 'index' is used for row-wise, whereas 1 or 'columns' is used for column-wise.
It is used to exclude all the null values.
It counts along a particular level and collapsing into a series, if the axis is a multiindex.
It includes only int, float, and boolean columns. If it is None, it will attempt to use everything, so numeric data should be used.
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.
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
Next TopicDataFrame.to_excel()
|