【问题标题】:Getting index from resample in Pandas从 Pandas 的重采样中获取索引
【发布时间】:2016-09-06 00:11:12
【问题描述】:

我在 Python 中有一个每秒频率的时间序列数据框。我正在尝试聚合数据以获得每分钟 Speed 的最大值。我正在使用此代码:

df = pd.DataFrame({ 'Speed' : [],
                  'Acceleration' : []
            })
rng = pd.date_range('1/1/2011', periods=72, freq='s')
df['Speed'] = np.random.randn(len(rng))
df['Acceleration'] = np.random.randn(len(rng))
df = df.set_index(rng)
df['Acceleration'].resample("1Min").max()

但是,我还有另一列 Speed,我有兴趣在每分钟将其关联值设为最大值 Acceleration。例如,假设 13:15 的最高 Acceleration 发生在 13:15:10,它是 1.2 m/s^2。同一秒,速度为5 m/s。除了最大加速度之外,我还想获得那个速度。谢谢。

【问题讨论】:

    标签: python pandas time-series resampling


    【解决方案1】:

    用你自己的例子试试这个

    In [17]: idx = df.resample('1Min').agg({'Acceleration': np.argmax})
    In [18]: df.ix[idx.Acceleration]
    Out[18]:
                         Acceleration     Speed
    2011-01-01 00:00:06      2.754047 -0.274572
    2011-01-01 00:01:06      3.258652 -1.302055
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2022-11-13
      • 1970-01-01
      • 2016-09-20
      • 2018-08-17
      • 2013-03-15
      • 2013-12-10
      • 2013-03-25
      • 1970-01-01
      相关资源
      最近更新 更多