10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1

n=len(f)

f1=np.reshape(f,[n/4,4])

ff=pd.DataFrame(f1,index=index,columns=['Mkt_Rf','SMB','HML','Rf'])

To view the first and last few observations for the dataset called ff, the functions of

.head() and .tail() can be used:

Data output

The simplest example is given here:

>>>f=open("c:/temp/out.txt","w")

>>>x="This is great"

>>>f.write(x)

>>>f.close()

For the next example, we download historical stock price data first, then write data

to an output file:

import re

from matplotlib.finance import quotes_historical_yahoo_ochl

ticker='dell'

outfile=open("c:/temp/dell.txt","w")

begdate=(2013,1,1)

enddate=(2016,11,9)

p=quotes_historical_yahoo_ochl

(ticker,begdate,enddate,asobject=True,adjusted=True)

outfile.write(str(p))

outfile.close()

[ 25 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!