一、csv
读取csv, index_col 较为常用;encoding = 'GBK' 在输入法出bug的时候可以添加,以中文读取;low_memory = False 在大内存的时候添加
pd.read_csv(" ***.csv ")
pd.read_csv(" ***.csv ", index_col = 0) : 将id列变index
pd.read_csv(" ***.csv ", index_col = 0, encoding = 'GBK' , low_memory = False )
输出: index是否要索引,header是否要列名,True就是需要
outputpath=path+'\数据\行业名称.csv'
industryname.to_csv(outputpath,index=True,header=True)
二、pickle的优势在于其速度
存入pickle文件:
import pickle
with open('***.pkl', 'wb') as file:
pickle.dump(rawdata1,file)
读pickle 文件:
with open('宏观定价***.pkl', 'rb') as file:
rawdata1=pickle.load(file)
三、excel优势在于多表格,劣势在于速度和内存
读取excel:注:应用程序在单斜杠层面可能出现错,可以使用双斜杠
方法1:
Excel = pd.ExcelFile(output_temp_path + "估值表数据汇总.xlsx")
position1 = Excel.parse("position", index_col=0) # 将position的表单抽出来
方法2:
position = pd.read_excel(path + "估值表数据汇总.xlsx", sheet_name = 'position', index_col = 0)
输出到excel:
方法1:
outputpath='E:\etf\离散度.xlsx'
compare.to_excel(outputpath,index=True,header=True,sheet_name='年数据')
方法二:多个excel:
with pd.ExcelWriter('E:\\python\\bollingersignal.xlsx') as writer:
log1.to_excel(writer,index=True,header=True,sheet_name='bollinger1')
log2.to_excel(writer,index=True,header=True,sheet_name='bollinger2')
writer.save()
writer.close()