Pandas DataFrame:滚动和扩展功能之间的区别 [英] Pandas DataFrame: difference between rolling and expanding function
本文介绍了Pandas DataFrame:滚动和扩展功能之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮助我从pandas文档中提供的示例中了解滚动功能和扩展功能之间的区别.
Can anyone help me understand the difference between rolling and expanding function from the example given in the pandas docs.
df = DataFrame({'B': [0, 1, 2, np.nan, 4]})
df
B
0 0.0
1 1.0
2 2.0
3 NaN
4 4.0
df.expanding(2).sum()
B
0 NaN # 0 + NaN
1 1.0 # 1 + 0
2 3.0 # 2 + 1
3 3.0 # ??
4 7.0 # ??
df.rolling(2).sum()
B
0 NaN # 0 + NaN
1 1.0 # 1 + 0
2 3.0 # 2 + 1
3 NaN # NaN + 2
4 NaN # 4 + NaN
我在每一行中都加注以表明我对计算的理解. rolling
函数是否正确? expanding
呢?第三和第四行中的3和7来自哪里?
I give comment to each row to show my understanding of the calculation. Is that true for rolling
function? What about expanding
? Where are 3 and 7 in 3rd and 4th rows coming from?
推荐答案
expanding
中的2是min_periods
而不是window
df.expanding(min_periods=1).sum()
Out[117]:
B
0 0.0
1 1.0
2 3.0
3 3.0
4 7.0
如果希望与rolling
window
相同的结果将等于数据帧的长度
If you want the same result with rolling
window
will be equal to the length of dataframe
df.rolling(window=len(df),min_periods=1).sum()
Out[116]:
B
0 0.0
1 1.0
2 3.0
3 3.0
4 7.0
这篇关于Pandas DataFrame:滚动和扩展功能之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文