Python|熊猫系列.sum()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas Series.sum()
方法用于获取请求轴的值的总和。
Syntax: Series.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0)
Parameters:
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
代码 #1:默认情况下,空或全 NA 系列的总和为 0。
# importing pandas module
import pandas as pd
# min_count = 0 is the default
pd.Series([]).sum()
# When passed min_count = 1,
# sum of an empty series will be NaN
pd.Series([]).sum(min_count = 1)
输出:
0.0
nan
代码#2:
# importing pandas module
import pandas as pd
# making data frame csv at url
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# sum of all salary
val = data['Salary'].sum()
val
输出:
2159837111.0
代码#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()
val
输出: