pandas GroupBy和组中前一行的累积平均值

2024-10-03 09:09:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,看起来像这样:

pd.DataFrame({'category': [1,1,1,2,2,2,3,3,3,4],
              'order_start': [1,2,3,1,2,3,1,2,3,1],
              'time': [1, 4, 3, 6, 8, 17, 14, 12, 13, 16]})
Out[40]: 
   category  order_start  time
0         1            1     1
1         1            2     4
2         1            3     3
3         2            1     6
4         2            2     8
5         2            3    17
6         3            1    14
7         3            2    12
8         3            3    13
9         4            1    16

我想创建一个新的列,其中包含相同类别以前的平均值。如何创建它?在

新列应该如下所示:

^{pr2}$

注:如果是第一次,平均值应为NaN。在

编辑:正如cs95所说,我的问题实际上与this one不一样,因为这里需要扩展。在


Tags: 数据编辑dataframetimeordernanoutthis
1条回答
网友
1楼 · 发布于 2024-10-03 09:09:25

“create a new column that containing the means of the previous times of the same category”听起来是GroupBy.expanding的一个很好的用例(还有一个移位):

df['mean'] = (
    df.groupby('category')['time'].apply(lambda x: x.shift().expanding().mean()))
df
   category  order_start  time  mean
0         1            1     1   NaN
1         1            2     4   1.0
2         1            3     3   2.5
3         2            1     6   NaN
4         2            2     8   6.0
5         2            3    17   7.0
6         3            1    14   NaN
7         3            2    12  14.0
8         3            3    13  13.0
9         4            1    16   NaN

另一种计算方法是不使用apply(链接两个groupby调用):

^{pr2}$

就表现而言,这实际上取决于团队的数量和规模。在

相关问题 更多 >