熊猫滚动窗口小于或等于

时间:2021-07-20 11:42:36

标签: pandas dataframe

我有一个基于三个维度分类的数据框:

>>> df
   a  b  c  d
0  a  b  c  1
1  a  e  x  2
2  a  f  e  3

当我通过以下命令滚动度量 d 时:

>>> df.d.rolling(window = 3).mean()
0    NaN
1    NaN
2    2.0
Name: d, dtype: float64

但我真正想要的是执行滚动 <= 给定的数字,如果第一个条目的结果是相同的数字本身,然后从第二个条目开始滚动,窗口大小为 1 并且为第三次滚动窗口大小为 2,从 3 开始滚动前 3 个窗口的运行平均值。

所以我期待的结果是:

对于数据框:

>>> df
   a  b  c  d
0  a  b  c  1
1  a  e  x  2
2  a  f  e  3

>>> df.d.rolling(window = 3).mean()
0    1 #Since this is the first one and so average of the first number is equal to number itself.
1    1.5 # Average of 1 and 2 as rolling criteria is <= 3
2    2.0 # Since here we have 3 elements so from here on it follows the general trend.
Name: d, dtype: float64

可以这样滚动吗?

1 个答案:

答案 0 :(得分:0)

我能够使用以下命令滚动:

>>> df.d.rolling(min_periods = 1, window = 3).mean()
0    1.0
1    1.5
2    2.0
Name: d, dtype: float64

min_periods 的帮助下,可以指定滚动窗口的最小配置数。