数据框中的累积或滚动产品

编程入门 行业动态 更新时间:2024-10-28 15:33:17
本文介绍了数据框中的累积或滚动产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有一列的数据框,我只是想添加另一列,该列采用原始列的滚动乘积.我已经搜索了一段时间,但这似乎是一种基本功能-不确定是否丢失了某些内容.想要将B列作为输出.

Hi I have a dataframe with a column and i'd simply like to add another column that takes the rolling product of the original column. I've been googling around for a while but this seems like such a basic functionality - not sure if I'm missing something. Id to like to get Column B as an output.

A B 1 1 2 2 3 6 4 24 5 120 6 720 7 5040

我本质上是在寻找类似的东西(如果存在)

Im essentially looking for something like this if it existed:

data ['B'] =数据['A'].rolling(window = 1).product()

data['B'] = data['A'].rolling(window=1).product()

我从较早时发现了这篇文章,但似乎使用的是rolling_apply,它不再活动了?:

I found this post from earlier but it seems to be using rolling_apply which is no longer active?:

如何在Pandas DataFrame上计算滚动累积产品 a>

我曾尝试在此处使用类似的解决方案,但似乎无法正常工作.

i've tried using a similar solution here like this but it doesn't seem to be working.

dftest= pd.DataFrame([1,2,3,4,5,6,7],columns=['A']) dftest['cum']=dftest['A'].rolling(1).apply(lambda x:x.prod())

输出:

A cumprod 0 1 1.0 1 2 2.0 2 3 3.0 3 4 4.0 4 5 5.0 5 6 6.0 6 7 7.0

推荐答案

好像您想要 cumprod

Seems like you want cumprod

df = pd.DataFrame({'v':[1,2,3,4,5,6]}) df['prod'] = df.v.cumprod() v prod 0 1 1 1 2 2 2 3 6 3 4 24 4 5 120 5 6 720

也可以

May also do

df.v.expanding().agg(lambda a:a.prod()) 0 1.0 1 2.0 2 6.0 3 24.0 4 120.0 5 720.0

更多推荐

数据框中的累积或滚动产品

本文发布于:2023-11-22 07:13:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框中   数据   产品

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!