2016-05-10 3 views
1

私は、毎秒の頻度で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であった。私は最大加速に加えてその速度を得たいと思います。ありがとう。

答えて

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 
1

ソリューション

import datetime as dt 
import numpy as np 
import pandas as pd 

np.random.seed([3, 1415]) 

seconds = pd.date_range('2016-03-01', '2016-03-02', freq='S') 

data = pd.Series(np.random.rand(len(seconds)), index=seconds) 

def minute(x): 
    """Returns a datetime object with seconds stripped off""" 
    fmt = '%Y%m%d%H%M' 
    return dt.datetime.strptime(x.strftime(fmt), fmt) 

data.groupby(minute).agg({'max': np.max, 'argmax': np.argmax}) 

あなたがあなた自身の状況にこれを適用する必要があります。

関連する問題