Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Retrieving data to our programs


To feed data to our programs, we need to understand how to input data. Since the data courses vary, we introduce several ways to input data, such as from clipboard, Yahoo! Finance, an external text or CSV file, a web page, and a MATLAB dataset.

Inputting data from the clipboard

In our everyday lives, we use Notepad, Microsoft Word, or Excel to input data. One of the widely used functionalities is copy and paste. The pd.read_clipboard() function contained in Pandas mimics this operation. For example, we type the following contents on Notepad:

x y
1 2 
3 4 
5 6

Then, highlight these entries, right-click on it, copy and paste in the Python console, and run the following two lines:

>>>import pandas as pd
>>>data=pd.read_clipboard()
>>>data
   X  y
1  2
3  4
5  6

This is true for copying data from Microsoft Word and Excel.

Retrieving historical price data from Yahoo! Finance

The following simple program has just five lines, and we can use...