从 Pandas 中的重采样获取索引

aho*_*osh 4 python time-series resampling pandas

我在 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()
Run Code Online (Sandbox Code Playgroud)

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

小智 5

用你自己的例子试试这个

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
Run Code Online (Sandbox Code Playgroud)