龙空技术网

Python 合并数据

divine11191963297 69

前言:

当前我们对“python合并数据框”都比较关怀,朋友们都需要知道一些“python合并数据框”的相关文章。那么小编同时在网摘上搜集了一些对于“python合并数据框””的相关内容,希望我们能喜欢,朋友们快快来学习一下吧!

Python 数据合并,仅供学习

import os

import random

import tushare as ts

import pandas as pd

import re

import time

import datetime

tk_lst=[';,]

pro=ts.pro_api()

rss="D:\\zDat\\min\\m01\\"

cnlst='D:\\zDat\\inx\\stk_base1.csv'

#cnlst="D:\\test.csv"

def single_down(xcod):

tim0='10191205'

time_temp = datetime.datetime.now() - datetime.timedelta(days=1)

end_dt = time_temp.strftime('%Y%m%d')

fss=rss+xcod+'.csv'

print('\n',fss);

xfg=os.path.exists(fss);xd0=[]

if xfg:

xd0=pd.read_csv(fss,index_col='trade_time',encoding="gbk")

if len(xd0)<1:

os.remove(fss)

tim0='10191205'

xd0=[]

else:

xd0=xd0.sort_index(ascending=False);

_xt=xd0.index[0];

s2=str(_xt);tim0=s2.split(" ")[0]

xd0=xd0.reset_index()

#print(xd0,xd0.tail())

try:

xd=ts.pro_bar(ts_code=xcod,asset='E',freq='1min',start_date=tim0,end_date=end_dt);

if xd is not None:

if (len(xd0)>0):

xd2 =xd0.append(xd)

xd2.drop_duplicates(subset='trade_time', keep='last', inplace=True);

xd=xd2;

xd.to_csv(fss,encoding='gbk',index=False)

except IOError:

pass #skip,error

df=pd.read_csv(cnlst,encoding='gbk')

tk_n=len(tk_lst)*2

random.shuffle(tk_lst)#tk_lst 随机化

for i in range(0,tk_n,2):

token=tk_lst[int(i/2)]

print(token)

ts.set_token(token)

dfi=df[i:i+2]

for xcod in dfi['ts_code']:

print(i,xcod)

single_down(xcod=xcod)

time.sleep(random.randint(3,9))

df[0:tk_n].to_csv("D:\\test.csv",encoding='gbk',index=False)

df[tk_n:].to_csv(cnlst,encoding='gbk',index=False)

标签: #python合并数据框