官术网_书友最值得收藏!

Data output

In the previous section, by using the R language, we generated a data asset called final. To save it to a CSV file, we could use the write.csv() or write.table() functions, shown in the code here:

> write.csv(final,file="c:/temp/t.csv") 
> write.table(final,file="c:/temp/t2.txt",sep=";") 

The separator for the write.csv() function is a comma, while we can specify our own separator for the write.table() function. To find out the other R functions starting with write, we could use the apropos() function, shown here:

> apropos("^write") 
 [1] "write"          "write.csv"      "write.csv2"     
 [4] "write.dcf"      "write.ftable"   "write.socket"   
 [7] "write.table"    "writeBin"       "writeChar"      
[10] "writeClipboard" "writeLines"     

For the following Python program, we export Fama-French monthly factors to three different output formats, pickle, CSV, and text files:

import pandas as pd 
infile="http://canisius.edu/~yany/data/ff3monthly.csv" 
ff3=pd.read_csv(infile,skiprows=3) 
print(ff3.head(2)) 
# output to pickle 
ff3.to_pickle("c:/temp/ff3.pkl") 
# output to a csv file  
outfile=open("c:/temp/ff3.csv","w") 
ff3.to_csv(outfile,index=None) 
outfile.close() 
# output to text file 
outfile2=open("c:/temp/ff3.txt","w") 
ff3.to_csv(outfile2, header=True, index=None, sep=' ', mode='a') 
outfile2.close() 
主站蜘蛛池模板: 湄潭县| 萨嘎县| 明溪县| 贵定县| 桂东县| 富宁县| 望江县| 兴安盟| 延川县| 若尔盖县| 黄冈市| 承德县| 榕江县| 商城县| 禹城市| 肇东市| 喜德县| 上虞市| 井研县| 武强县| 平武县| 青海省| 桑植县| 巴青县| 新民市| 上蔡县| 孝感市| 东乌珠穆沁旗| 霍林郭勒市| 台南市| 盘山县| 景德镇市| 香格里拉县| 长治市| 高平市| 交城县| 历史| 巴东县| 闽清县| 双辽市| 城固县|