Applying lambda function to a pandas rolling window series

。_饼干妹妹 提交于 2019-12-06 04:15:48

IIUC, if all you want to do is apply a function on the window, your second attempt comes close:

rolling_window.apply(lambda x: np.random.choice(x, size=1))

However, you can circumvent the use of the lambda like this:

rolling_window.apply(np.random.choice, kwargs={'size' : 1})

0    NaN
1    NaN
2    1.0
3    4.0
4    4.0
5    5.0
6    7.0
7    7.0
8    8.0
dtype: float64

Additional arguments to the function you pass go in args and kwargs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!