K線(xiàn)數(shù)據(jù)提取
依據(jù)原有數(shù)據(jù)集格式,按要求生成新表:
1、每分鐘的close數(shù)據(jù)的第一條、最后一條、最大值及最小值,
2、每分鐘vol數(shù)據(jù)的增長(zhǎng)量(每分鐘vol的最后一條數(shù)據(jù)減第一條數(shù)據(jù))
3、匯總這些信息生成一個(gè)新表
(字段名:[‘time',‘open',‘close',‘high',‘low',‘vol'])
import pandas as pd import time start=time.time() df=pd.read_csv('data.csv') df=df.drop('id',axis=1) #刪除id列 df1=pd.DataFrame(columns=['time','open','close','high','low','vol'])#新建目標(biāo)數(shù)據(jù)表 for i in df.groupby('time'): #按時(shí)間分組 new_df=pd.DataFrame(columns=['time','open','close','high','low','vol']) #新建空表用于臨時(shí)轉(zhuǎn)存要求數(shù)據(jù) new_df.time=i[1].time[0:1] #取每組時(shí)間為新表時(shí)間 new_df.open=i[1].close[0:1] #取每組第一個(gè)close數(shù)據(jù)為新表open數(shù)據(jù) new_df.close=i[1]['close'].iloc[-1] #取每組最后一個(gè)close數(shù)據(jù)為新表close數(shù)據(jù) new_df.high=i[1]['close'].max() #取每組close數(shù)據(jù)最大值為新表hige數(shù)據(jù) new_df.low=i[1]['close'].min() #取每組close數(shù)據(jù)最小值為新表low數(shù)據(jù) new_df.vol=i[1]['vol'].iloc[-1] - i[1]['vol'].iloc[0] #用每組vol數(shù)據(jù)最大值減去最小值為新表vol數(shù)據(jù) df1=pd.concat([new_df,df1],axis=0) #縱向合并數(shù)據(jù)到目標(biāo)數(shù)據(jù)表 df2=df1.sort_values('time') #按time列值進(jìn)行排序 df2.reset_index(inplace=True, drop=True) #重置行索引 print(df2) #打印目標(biāo)數(shù)據(jù)表 stop=time.time() #查看耗時(shí) print('共計(jì)耗時(shí):{}秒'.format(stop-start))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
