猿问

将具有多个参数的函数应用于滚动 DataFrame Pandas

我有一个类似于下面的 Pandas DataFrame。请注意,这不需要采用这种格式,可以将其放回旋转方式,variable如果这样更容易,可以将每个列作为一列。


      date      variable  value

0   2014-01-31    item1  0.070898

1   2014-02-28    item1  0.064725

2   2014-03-31    item1  0.101292

3   2014-04-30    item1  0.041898

4   2014-05-31    item1  0.094894

5   2014-06-30    item1  0.110287

6   2014-07-31    item1  0.081844

7   2014-08-31    item1  0.043819

8   2014-09-30    item1  0.009196

9   2014-10-31    item1  0.076482

10  2014-11-30    item1  0.055906

11  2014-12-31    item1  0.042028

12  2015-01-31    item1  0.115469

13  2015-02-28    item1  0.130457

14  2015-03-31    item1  0.054314

15  2015-04-30    item1  0.141286

16  2015-05-31    item1  0.109387

17  2015-06-30    item1  0.066154

18  2015-07-31    item1  0.039390

19  2015-08-31    item1  0.071276

20  2015-09-30    item1  0.088784

21  2015-10-31    item1  0.054234

22  2015-11-30    item1  0.111854

23  2015-12-31    item1  0.005053

24  2016-01-31    item1  0.145953

25  2016-02-29    item1  0.138051

26  2016-03-31    item1  0.030395

27  2016-04-30    item1  0.055843

28  2016-05-31    item1  0.037960

29  2016-06-30    item1  0.147318

..         ...      ...       ...

120 2015-09-30      ref  0.043185

121 2015-10-31      ref  0.046849

122 2015-11-30      ref  0.008139

123 2015-12-31      ref  0.011222

124 2016-01-31      ref  0.026408

125 2016-02-29      ref  0.040404

126 2016-03-31      ref  0.039585

127 2016-04-30      ref  0.028782

128 2016-05-31      ref  0.002799

129 2016-06-30      ref  0.040413

130 2016-07-31      ref  0.004451

131 2016-08-31      ref  0.001946

132 2016-09-30      ref  0.029548

对于中的每个项目,variable我想应用一个滚动 12 个月的计算,该计算接受value该项目的滚动 12 个月和滚动 12 个月的值作为参数,其中variableequals ref。该函数的形式为:


def func(series1,series2):

   do something with series

   return single_value_from_doiong_something

我的问题是如何将两个不同的系列传递给这个函数来返回我的单个值。有谁知道如何做到这一点?


HUH函数
浏览 91回答 1
1回答

胡子哥哥

关于您提到的功能,在您使用 a 重构原始数据框后join(),这可能更接近您所追求的:combined = df[df['variable']!='ref'].set_index('date').join(df[df['variable']=='ref'].set_index('date'), lsuffix='', rsuffix='_ref').drop('variable_ref', axis=1)def func(series, ref_series):    #As an example    return series.mean()/ref_series.mean()combined.groupby('variable').rolling(12).apply(lambda x: func(x, combined.loc[x.index]['value_ref']), raw=False).drop('value_ref', axis=1)此示例产生以下结果(NaN由于您的示例数据中存在差距):                        valuevariable date                item1    2014-01-31       NaN         2014-02-28       NaN         2014-03-31       NaN         2014-04-30       NaN         2014-05-31       NaN         2014-06-30       NaN         2014-07-31       NaN         2014-08-31       NaN         2014-09-30       NaN         2014-10-31       NaN         2014-11-30       NaN         2014-12-31       NaN         2015-01-31       NaN         2015-02-28       NaN         2015-03-31       NaN         2015-04-30       NaN         2015-05-31       NaN         2015-06-30       NaN         2015-07-31       NaN         2015-08-31       NaN         2015-09-30  1.912186         2015-10-31  1.793184         2015-11-30  2.609254         2015-12-31  3.009455         2016-01-31  3.123833         2016-02-29  2.910599         2016-03-31  2.708132         2016-04-30  2.497878         2016-05-31  2.561760         2016-06-30  2.681712
随时随地看视频慕课网APP

相关分类

Python
我要回答