[英]Pandas Dynamic Index Referencing during Calculation
I have the following data frame 我有以下数据框
val sum
0 1 0
1 2 0
2 3 0
3 4 0
4 5 0
5 6 0
6 7 0
I would like to calculate the sum of the next three rows' (including the current row) values. 我想计算接下来的三行(包括当前行)值的总和。 I need to do this for very big files.
对于大型文件,我需要这样做。 What is the most efficient way?
最有效的方法是什么? The expected result is
预期的结果是
val sum
0 1 6
1 2 9
2 3 12
3 4 15
4 5 18
5 6 13
6 7 7
In general, how can I dynamically referencing to other rows (via boolean operations) while making assignments? 通常,在进行分配时,如何动态引用其他行(通过布尔操作)?
> pd.rolling_sum(df['val'], window=3).shift(-2)
0 6
1 9
2 12
3 15
4 18
5 NaN
6 NaN
If you want the last values to be "filled in" then you'll need to tack on NaN's to the end of your dataframe. 如果要“填充最后一个值”,则需要将NaN附加到数据框的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.