2017-04-23 91 views
1
     open high low  close 
date     
2017-04-10 09:01:00 3992.0 3992.0 3982.0 3984.0 
2017-04-10 09:02:00 3985.0 3988.0 3980.0 3986.0 
2017-04-10 09:03:00 3986.0 3986.0 3973.0 3977.0 
2017-04-10 09:04:00 3977.0 3983.0 3974.0 3981.0 
2017-04-10 09:05:00 3980.0 3980.0 3956.0 3961.0 
2017-04-10 09:06:00 3961.0 3968.0 3959.0 3964.0 

上面每分鐘都會生成一些股票數據。 我想將它們分組,每組15行,然後將ohlc函數應用於每個組。有什麼好方法可以做到嗎?提前致謝。帶有行數的大熊貓數據

+0

你可以發佈你已經嘗試什麼,即使它是錯的? –

+0

15行,你的意思是15分鐘?如果是這樣,那麼開放和高點會是什麼?最大/最小打開或第一次打開?同樣的問題關閉。 – pshep123

回答

1

我懷疑沿着線的東西應該工作:

df.resample('15Min').agg({'open': 'first', 
          'high': 'max', 
          'low': 'min', 
          'close': 'last'}) 

證明

df 
        open high low  close 
date     
2017-04-10 09:01:00 3992.0 3992.0 3982.0 3984.0 
2017-04-10 09:02:00 3985.0 3988.0 3980.0 3986.0 
2017-04-10 09:03:00 3986.0 3986.0 3973.0 3977.0 
2017-04-10 09:04:00 3977.0 3983.0 3974.0 3981.0 
2017-04-10 09:05:00 3980.0 3980.0 3956.0 3961.0 
2017-04-10 09:06:00 3961.0 3968.0 3959.0 3964.0 

df.resample('2Min').agg({'open': 'first', 
          'high': 'max', 
          'low': 'min', 
          'close': 'last'}) 

        open high low  close 
date     
2017-04-10 09:00:00 3992.0 3992.0 3982.0 3984.0 
2017-04-10 09:02:00 3985.0 3988.0 3973.0 3977.0 
2017-04-10 09:04:00 3977.0 3983.0 3956.0 3961.0 
2017-04-10 09:06:00 3961.0 3968.0 3959.0 3964.0 
+0

感謝您的工作,解決了我的問題。 – michael

+0

您可能會考慮加註並接受解決方案 –

0

感謝@Sergey Bushmanov。它解決了我的問題。 並添加更多爲我的特殊情況。由於數據中存在一些時間差異,所以我不能直接使用resample。

我在上面的代碼中嘗試了這個基礎。結果很好。

df.reset_index(inplace=True) 
groupby_list=list((df.index/15).map(lambda x:int(x))) 
df.groupby(groupby_list).agg({ 
     'code':'first','date':'first', 'open': 'first', 
          'high': 'max', 
          'low': 'min', 
          'close': 'last'}) 

結果:

open high code close low date 
0 3992.0 3992.0 jd1709 3916.0 3914.0 2017-04-10 09:01:00 
1 3916.0 3935.0 jd1709 3920.0 3913.0 2017-04-10 09:16:00 
2 3919.0 3926.0 jd1709 3921.0 3910.0 2017-04-10 09:31:00 
3 3921.0 3923.0 jd1709 3920.0 3913.0 2017-04-10 09:46:00 
4 3919.0 3928.0 jd1709 3927.0 3917.0 2017-04-10 10:01:00 
5 3926.0 3932.0 jd1709 3927.0 3923.0 2017-04-10 10:31:00 
6 3927.0 3928.0 jd1709 3915.0 3912.0 2017-04-10 10:46:00 
7 3916.0 3924.0 jd1709 3918.0 3912.0 2017-04-10 11:01:00 
8 3919.0 3925.0 jd1709 3916.0 3916.0 2017-04-10 11:16:00 
9 3917.0 3928.0 jd1709 3924.0 3912.0 2017-04-10 13:31:00