Python for Finance - Niedermayer
[Pages:52]Python for Finance
Data Analysis with Pandas
Andras Niedermayer
Outline
1 Basic I/O in Python: pickle and csv files.
2 The pandas library. I/O operations in pandas The DataFrame class
3 Working with datetime objects
4 High-frequency data
5 Data visualization: matplotlib 2D-plots: single series 2D-plots: multiple series and figures Financial plots: matplotlib.finance
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 2/52
Writing objects to disk: pickle
First, let us generate a list of 10,000 numbers from the standard normal distribution.
1 import numpy as np 2 from random import gauss 3 a=[gauss(0,1) for i in range (10000)]
Then, we save it to a pickle file:
1 import pickle 2 # open file with write permission 3 file=open('data.pkl','wb') 4 # dump variable into file 5 pickle.dump(a, file) 6 # close file 7 file.close()
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 3/52
Writing objects to disk: pickle
A better way (so you don't have to remember to close the file):
1 import pickle
2 # open file with write permission
3 with open('data.pkl','wb') as file:
4
# dump variable into file
5
pickle.dump(a, file)
6 # file automatically closed at end of block
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 4/52
Loading objects from disk: pickle
To load the pickle file from disk: 1 # open file 2 file=open('data.pkl','rb') 3 # load variable from file 4 b=pickle.load(file) 5 file.close()
1 You can store more than one object in a pickle. 2 However, they are retrieved sequentially (FIFO):
First object you load is the first object you saved, etc. 3 Creating a dictionary (dict) of all objects helps indexing all
objects in the pickle.
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 5/52
Loading objects from disk: pickle
A better way to load the pickle file from disk:
1 # open file
2 with open('data.pkl','rb') as file:
3
# load variable from file
4
b=pickle.load(file)
5 # file closed automatically
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 6/52
Writing objects to disk: csv
We generate a 5 by 3 matrix of numbers from the standard normal distribution. Row names are from A to E.
1 m=np.random.standard_normal ((5,3)) 2 rownames=['A', 'B', 'C', 'D', 'E']
We create a header and save both the data and row names to csv:
1 # generate header
2 header="row name , v1 , v2 , v3\n"
3 # open file
4 with open('data.csv','w') as csv_file:
5
csv_file.write(header)
6
# add data row -by -row
7
for r_ , (v1 , v2 , v3) in zip(rownames ,m):
8
s="%s, %f, %f, %f\n" %(r_ ,v1 ,v2 ,v3)
9
# 1 string , 3 floats
10
csv_file.write(s) # write the row
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 7/52
Loading objects from disk: csv
Read the csv line by line:
1 with open('data.csv','r') as csv_file:
2
for i in range (5):
3
print ( csv_file . readline ())
Read the csv all at once:
1 with open('data.csv','r') as csv_file:
2
content = csv_file . readlines ()
Wednesday, February, 2019 Andras Niedermayer - Universit?e Paris-Dauphine
Python for Finance - Lecture 5 8/52
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- pandas cheat sheet pandas python data analysis
- sort values by multiple columns
- python for finance
- python for finance niedermayer
- pandas cheat sheet pandas python data analysis library
- csv editing with python and pandas
- modellinghumaninteractions acrossacitywithgraphs
- hello graphics world basic charts in sas ods graphics
- pyarrow documentation read the docs
- python panda count rows
Related searches
- cars for finance near me
- dynamics 365 for finance and operations
- job description for finance manager
- resume examples for finance manager
- job description for finance officer
- job specification for finance manager
- best practices for finance departments
- microsoft dynamics 365 for finance and operations
- entry level jobs for finance majors
- calculator for finance class
- careers for finance majors
- cover letter for finance jobs