【问题标题】:Calculate the Mean of last 3 records in a timeseries data in pandas计算熊猫时间序列数据中最后3条记录的平均值
【发布时间】:2019-03-28 12:41:06
【问题描述】:

在计算我的时间序列数据框中最后 3 条记录的平均值时面临问题。以下是数据样本

serial,date,feature1,,,,,,,,,,,,,,,,,
1,5/19/2017,-5.199338,,,,,,,,,,,,,,,,,
5,6/12/2017,-25.199338,,,,,,,,,,,,,,,,,
5,6/23/2017,5.199338,,,,,,,,,,,,,,,,,
2,7/1/2017,8.199338,,,,,,,,,,,,,,,,,
1,7/17/2017,3.199338,,,,,,,,,,,,,,,,,
1,7/29/2017,76.199338,,,,,,,,,,,,,,,,,
2,8/19/2017,13.199338,,,,,,,,,,,,,,,,,
6,9/19/2017,785.199338,,,,,,,,,,,,,,,,,
3,10/28/2017,5.199338,,,,,,,,,,,,,,,,,
4,11/2/2017,67.199338,,,,,,,,,,,,,,,,,
2,11/28/2017,49.199338,,,,,,,,,,,,,,,,,
2,12/29/2017,20.199338,,,,,,,,,,,,,,,,,
3,1/29/2018,19.199338,,,,,,,,,,,,,,,,,
4,3/13/2018,-15.199338,,,,,,,,,,,,,,,,,
1,3/28/2018,-5.199338,,,,,,,,,,,,,,,,,

要求是在数据框中添加另一列 mean,这将是最后 3 行具有相似 serial 数字的平均值(对于列 feature1)。必须对每一行执行此操作。

例如计算下一行的平均值

1,3/28/2018,-5.199338,,,,,,,,,,,,,,,,,

将使用以下数据集完成 -

1,7/17/2017,3.199338,,,,,,,,,,,,,,,,,
1,7/29/2017,76.199338,,,,,,,,,,,,,,,,,
1,3/28/2018,-5.199338,,,,,,,,,,,,,,,,,

计算后的平均行数

serial,date,feature1,mean_feature1,,,,,,,,,,,,,,,,,
...........................
1,3/28/2018,-5.199338,24.7333,,,,,,,,,,,,,,,,

我的问题陈述与下面的文章类似,但它使用的是滚动,这需要确定的窗口,在我的情况下是随机的 - Pandas: Average value for the past n days

预期输出 -

serial,date,feature1,mean_feature1,,,,,,,,,,,,,,,,
1,5/19/2017,-5.199338,-5.199338,,,,,,,,,,,,,,,,
5,6/12/2017,-25.199338,-25.199338,,,,,,,,,,,,,,,,
5,6/23/2017,5.199338,-10.0,,,,,,,,,,,,,,,,
2,7/1/2017,8.199338,8.199338,,,,,,,,,,,,,,,,
1,7/17/2017,3.199338,-1,,,,,,,,,,,,,,,,
1,7/29/2017,76.199338,24.xxx,,,,,,,,,,,,,,,,
2,8/19/2017,13.199338,10.7xx,,,,,,,,,,,,,,,,
6,9/19/2017,785.199338,785.199338,,,,,,,,,,,,,,,,
3,10/28/2017,5.199338,5.199338,,,,,,,,,,,,,,,,
4,11/2/2017,67.199338,67.199338,,,,,,,,,,,,,,,,
2,11/28/2017,49.199338,23.xxx,,,,,,,,,,,,,,,,
2,12/29/2017,20.199338,27.xx,,,,,,,,,,,,,,,,
3,1/29/2018,19.199338,12.xxx,,,,,,,,,,,,,,,,
4,3/13/2018,-15.199338,26.xxxx,,,,,,,,,,,,,,,,
1,3/28/2018,-5.199338,24.xxxxx,,,,,,,,,,,,,,,,

请注意,“mean_feature1”列的值是近似计算的

【问题讨论】:

  • 如何确定“相似序列号”?
  • @Cut7er,类似的序列号表示列中的值(数据集中的第一列)

标签: python pandas numpy


【解决方案1】:

您需要groupbyrollingmean

#if necessary remove only NaNs columns
df = df.dropna(how='all', axis=1)
df['mean_feature1'] = (df.groupby('serial',sort=False)['feature1']
                        .rolling(3, min_periods=1).mean()
                        .reset_index(drop=True))
print (df)

    serial        date    feature1  mean_feature1
0        1   5/19/2017   -5.199338      -5.199338
1        5   6/12/2017  -25.199338     -25.199338
2        5   6/23/2017    5.199338     -10.000000
3        2    7/1/2017    8.199338       8.199338
4        1   7/17/2017    3.199338      -1.000000
5        1   7/29/2017   76.199338      24.733113
6        2   8/19/2017   13.199338      10.699338
7        6   9/19/2017  785.199338     785.199338
8        3  10/28/2017    5.199338       5.199338
9        4   11/2/2017   67.199338      67.199338
10       2  11/28/2017   49.199338      23.532671
11       2  12/29/2017   20.199338      27.532671
12       3   1/29/2018   19.199338      12.199338
13       4   3/13/2018  -15.199338      26.000000
14       1   3/28/2018   -5.199338      24.733113

如果想要insert 按位置列:

df.insert(3, 'mean_feature1', (df.groupby('serial',sort=False)['feature1']
                                 .rolling(3, min_periods=1).mean()
                                 .reset_index(drop=True)))

【讨论】:

  • 我认为你需要在 apply() 之前在 groupby 之后对日期进行排序。
  • 我正在寻找的是在我的原始数据框中为所有记录添加一列..
  • @JagrutTrivedi - 需要df['mean_feature1'] = df.groupby('serial',sort=False)['feature1'].transform(lambda x: x.tail(3).mean()) 吗?预期输出是什么?
  • @JagrutTrivedi - 或者需要df['mean_feature1'] = df.groupby('serial',sort=False)['feature1'].rolling(3, min_periods=1).mean().reset_index(level=0, drop=True)
猜你喜欢
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2018-10-26
  • 2019-11-09
  • 2017-08-03
  • 2019-07-18
  • 2021-07-21
  • 2020-01-15
相关资源
最近更新 更多