Pandas - 分组滚动均值

6

我是Pandas的新手。 我有一个数据框,其中我正在查看赛马结果。 我尝试为每匹马的最后30天完成结果的位置列获取滚动均值。以下是数据框中两匹马的示例:

        Horse            Position  OR   RaceDate    Weight
125283  cookie ring             4  59.0 2016-04-25  52.272727
126134  a boy named sue         7  46.0 2016-05-31  54.090909
137654  a boy named sue         4  49.0 2017-01-25  57.727273
138434  a boy named sue         8  48.0 2017-02-04  55.909091
138865  a boy named sue         2  48.0 2017-02-10  51.363636
140720  a boy named sue         3  50.0 2017-03-10  54.545455
141387  a boy named sue         7  49.0 2017-03-22  59.545455
143850  cookie ring             11  54.0 2017-05-25 56.818182
144203  cookie ring             9  54.0 2017-06-03  50.000000

我需要对每匹马进行分组,然后对其应用90天的滚动平均值。我将通过调用以下内容来实现:

df['PositionAv90D'] = df.set_index('RaceDate').groupby('Horse').rolling("90d")['Position'].mean().reset_index()

但这会返回一个有3列的数据框,并且仍然索引到马。以下是示例:

0          a b celebration 2011-08-24       3.000000
1          a b celebration 2011-09-15       4.500000
2          a b celebration 2012-05-29       4.000000
3        a beautiful dream 2016-10-21       2.333333
4        a big sky brewing 2008-04-11       2.000000
5        a big sky brewing 2008-07-08       7.500000
6        a big sky brewing 2008-08-11      10.000000
7        a big sky brewing 2008-09-20       9.000000
8        a big sky brewing 2008-12-30       4.333333
9        a big sky brewing 2009-01-21       3.666667
10       a big sky brewing 2009-02-20       3.777778

我需要一个与原始数据框相同索引的列。
你能帮忙吗?

你期望什么?此外,如果你的示例数据匹配,那会更有帮助。尝试获取数据框的一个小样本,并在该小样本上执行计算。 - busybear
1个回答

8

使用set_index()会删除原始索引,因此请先使用reset_index(),它将创建一个名为“index”的新列,其中包含您的原始索引。然后,不要在最后使用reset_index()(它只会创建索引0、1、2等),而是使用set_index('index')返回原始索引。

因此,如果您按照以下步骤操作,我认为它会起作用:

df['PositionAv90D'] = df.reset_index().set_index('RaceDate').groupby('Horse').rolling("90d")['Position'].mean().set_index('index')

有一个简单的数据样本会很好地进行测试,从你给出的内容中重新创建有点困难。

编辑1:

由于您正在切换索引,因此将其拆分为几部分更容易。请参见下面,我创建了一些示例数据,我认为这与您所说的类似:

df = pd.DataFrame({'foo': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   'bar': [1, 1, 1, 2, 2, 2, 3, 3, 3],
                   'baz': [11, 12, 13, 14, 15, 16, 17, 18, 19]},
                  index = [14, 15, 16, 17, 18, 19, 20, 21, 22])

df.reset_index(inplace=True)    # This gives us index 0,1,2... and a new col 'index'
df.set_index('baz', inplace=True)    # Replace with date in yours
# This next bit does the groupby and rolling, which will give a df 
# with a multi index of foo and baz, then reset_index(0) to remove the foo index level
# so that it matches the original df index so that you can add it as a new column
df['roll'] = df.groupby('foo')['bar'].rolling(3).sum().reset_index(0,drop=True)
df.reset_index(inplace=True)    # brings baz back into the df as a column
df.set_index('index', inplace=True)   # sets the index back to the original

这将为原始数据框添加一个新列,其中包含滚动值。在我的示例中,每组的前两个值将为NaN,因为窗口仅从idx = window size开始。因此,在您的情况下,每个组的前89天将为NaN。您可能需要添加一个额外的步骤,从生成的DataFrame中选择最后30天。

实际上这样做行不通,因为当你执行 rolling("90d")['Position'] 时,你只选择了每个组中的位置列,所以你只会得到索引、组列和位置列,这就是为什么你只看到返回了3个(因为你已经执行了 reset_index(),所以有一个额外的新索引)。 - S.B.G
已添加更新以保留结果数据框中的原始列。 - S.B.G
这是一个隐藏的宝藏:“使用 set_index() 将删除原始索引” - jtlz2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接