简体   繁体   中英

Easiest way to iterate through a dataframe and get the product of the last 30 days

如果我有一个用DateTime列索引的数据帧,而A列填充了浮点数,那将是创建B列的最有效方法,B列的值是该特定值的所有值的所有单个乘积排回30天。

Use the rolling_apply function of pandas:

df['B'] = rolling_apply(df.A, 30, numpy.prod)

I assume your index is on a daily basis, otherwise, you need to modify the second parameter
which I set to 30 .

pandas features a lot of standard statistics for so-called rolling horizons, such as a rolling mean, or a rolling standard deviation. The product is not part of it as far as I can see, so you have to define it yourself in the above way. For further information, see the pandas doc on computing individual statistics .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM