【问题标题】:Rolling 10 minutes recent value in dataframe在数据框中滚动 10 分钟的最近值
【发布时间】:2020-07-23 06:51:44
【问题描述】:

我希望根据给定的列值创建一个新列。 “CurrentValue”列的每一行都应等于“InitialValue”列在过去 10 分钟内的最新值。

这是数据集(csv格式):

date,InitialValue
3/20/2020 1:00,
3/20/2020 1:01,
3/20/2020 1:02,
3/20/2020 1:03,
3/20/2020 1:04,
3/20/2020 1:05,
3/20/2020 1:07,
3/20/2020 1:12,
3/20/2020 1:13,
3/20/2020 1:15,
3/20/2020 1:16,555
3/20/2020 1:17,
3/20/2020 1:19,
3/20/2020 1:20,
3/20/2020 1:22,
3/20/2020 1:26,576
3/20/2020 1:27,
3/20/2020 1:28,
3/20/2020 1:34,
3/20/2020 1:35,
3/20/2020 1:36,
3/20/2020 1:37,
3/20/2020 1:38,577
3/20/2020 1:40,
3/20/2020 1:42,
3/20/2020 1:43,
3/20/2020 1:44,
3/20/2020 1:45,
3/20/2020 1:51,

这是示例输出:

date,InitialValue,CurrentValue
2020-03-20 01:00:00,,
2020-03-20 01:01:00,,
2020-03-20 01:02:00,,
2020-03-20 01:03:00,,
2020-03-20 01:04:00,,
2020-03-20 01:05:00,,
2020-03-20 01:07:00,,
2020-03-20 01:12:00,,
2020-03-20 01:13:00,,
2020-03-20 01:15:00,,
2020-03-20 01:16:00,555.0,555.0
2020-03-20 01:17:00,,555.0
2020-03-20 01:19:00,,555.0
2020-03-20 01:20:00,,555.0
2020-03-20 01:22:00,,555.0
2020-03-20 01:26:00,576.0,576.0
2020-03-20 01:27:00,,576.0
2020-03-20 01:28:00,,576.0
2020-03-20 01:34:00,,576.0
2020-03-20 01:35:00,,576.0
2020-03-20 01:36:00,,576.0
2020-03-20 01:37:00,,
2020-03-20 01:38:00,577.0,577.0
2020-03-20 01:40:00,,577.0
2020-03-20 01:42:00,,577.0
2020-03-20 01:43:00,,577.0
2020-03-20 01:44:00,,577.0
2020-03-20 01:45:00,,577.0
2020-03-20 01:51:00,,

更新:这不是正确答案Pandas - Using 'ffill' on values other than Na

更新 2:输出数据已更新

【问题讨论】:

  • 您的示例输出与您的描述不符,因为值被向前填充超过 10 分钟。您能否更新示例输出或澄清问题?
  • 更新了输出数据样本

标签: python pandas dataframe time-series analytics


【解决方案1】:
import pandas as pd
import datetime
import numpy as np

df = pd.read_csv('filename.csv')
df['CurrentValue']=np.NaN

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
ten_minutes = datetime.timedelta(minutes=10)

for row in df.iterrows():
    df_timed = df[row[0]-ten_minutes: row[0]]
    for k in df_timed.iloc[::-1].iterrows():
        if not pd.isnull(k[1]['InitialValue']):
            df.at[row[0],'CurrentValue'] = k[1]['InitialValue']
            break

【讨论】:

    【解决方案2】:

    我假设 df['date'] 是日期时间类型。如果是字符串,先通过

    转换
    df['date'] = pd.to_datetime(df['date'])
    

    解决方案 1(更短):

    使用pd.DataFrame.rolling 偏移 10 分钟。

    df = df.set_index('date')
    df['CurrentValue'] = df.rolling('10min',closed='both')['InitialValue'].apply(lambda x: x.ffill()[-1])
    

    解决方案 2(更快):

    查找每行最后一次观察的日期和值

    # get date of last observation
    lastDate = df['date'].mask(pd.isnull(df['InitialValue']))
    lastDate = lastDate.ffill()
        
    # fill latest observation into CurrentValue if lastDate is less than 600s old
    seconds_since_last = (df['date'] - lastDate).dt.total_seconds()
    df['CurrentValue'] = df['InitialValue'].ffill().mask(seconds_since_last > 600)
    

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 1970-01-01
      • 2023-02-09
      • 2019-08-29
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多