pandas.Series.cumprod 官方文档
cumprod()累积连乘
Series.cumprod(axis=None, skipna=True, *args, **kwargs) #实现功能:Return cumulative product over a DataFrame or Series axis. #实现功能:Returns a DataFrame or Series of the same size containing the cumulative product. #return:scalar or Series
cumsum()累积连加
pandas.Series.prod官方文档
Series.prod(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs) # 实现功能:Return the product of the values for the requested axis. # return:scalar or Series
优点没看明白,因为常规情况下,所用的.prod()并非pandas下的函数,而是numpy下的函数。
numpy.prod官方文档
numpy.prod(a, axis=None, dtyp<i style="color:transparent">本文来源gaodai$ma#com搞$代*码6网</i>e=None, out=None, keepdims=<class numpy._globals._NoValue>) # 实现功能:Return the product of array elements over a given axis. # return:product_along_axis : ndarray
返回给定轴上数组元素的乘积。
跟cumprod不同,cumprod是计算当前一个累积乘上前面所有的数据,更多是一个list;prod返回的是给定这个轴上最终一个值。
补充:【python初学者】简单易懂的图解:np.cumsum和np.cumprod函数到底在干嘛?
1.np.cumsum
本人是一名python小白,最近过完了python的基本知识后,在看《利用python进行数据分析》这本书,书中cumsum函数一笔带过留下本小白“懵逼树下你和我”,当然是我自己的问题不是书的问题,经过画图理解后渐渐明白了这个函数到底在干么。
1.1np.cumsum-轴的概念
首先,在学习cumsum函数之前我们应该先明白什么是轴,以下面代码来进行说明:
arr=np.arange(1,17,1).reshape((2,2,4)) arr
array([[[ 1, 2, 3, 4], [ 5, 6, 7, 8]], [[ 9, 10, 11, 12], [13, 14, 15, 16]]])
其实数组的轴(axis)就是数组的维度,上面的代码生成了一个224的数组,所以
1、这个数组的0轴为2 ,axis=0
2、这个数组的1轴为2 ,axis=1
3、这个数组的2轴为4 ,axis=2
该数组如图所示(蓝,橙,黄,绿都是2轴,橙和绿上的“2轴”画图时忘了标注):
这里还要补充说一下:红色的数字只是因为我用的iPad画图很不方便所以没改成黑色,忽略就好
1.2cumsum(axis=0)
cumsum作用:计算轴向元素累加和,返回由中间结果组成的数组
这句概念中我认为大家理解起来比较难受的地方应该是轴向元素累加。
首先,通过前文对轴概念的理解我们可以知道
axis=0代表着最外层的维度也就是0轴(这里可能说法不太正确,主要为了配合上节图片),所以就是0轴的累加计算,我们以前文用到的数组为例(红色虚线表示按照0轴进行累加):