We use the display function to render the html.
from IPython.core.display import HTML
HTML("<iframe src=http://pandas.pydata.org width=600 height=350></iframe>")
Pandas has its own representation to have this appearance in the iPython Notebook.
import pandas as pd
import pandas.io.data
import datetime
aapl = pd.io.data.get_data_yahoo('AAPL',
start=datetime.datetime(2006, 10, 1),
end=datetime.datetime(2015, 1, 4))
aapl.head()
Open | High | Low | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2006-10-02 | 75.10 | 75.87 | 74.30 | 74.86 | 178159800 | 10.09 |
2006-10-03 | 74.45 | 74.95 | 73.19 | 74.08 | 197677200 | 9.98 |
2006-10-04 | 74.10 | 75.46 | 73.16 | 75.38 | 207270700 | 10.16 |
2006-10-05 | 74.53 | 76.16 | 74.13 | 74.83 | 170970800 | 10.08 |
2006-10-06 | 74.42 | 75.04 | 73.81 | 74.22 | 116739700 | 10.00 |
The data can be save into a CSV file.
aapl.to_csv('aapl_ohlc.csv')
%ls
00_Basics.ipynb
01_numpy.ipynb
02_Pandas.ipynb
02_audio.ipynb
03_R.ipynb
04_Cython.ipynb
DataLab_final_logo__ColourURL_--UPDATED.png
aapl_ohlc.csv
deepscore_cnn.py
deepscore_simple_1/
mnist.pkl.gz
mnist.py
mnist.pyc
mnist_conv.py
png2pickle.ipynb
title.ipynb
Lets read it back!
df = pd.read_csv('aapl_ohlc.csv', index_col='Date', parse_dates=True)
df.tail()
Open | High | Low | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2014-12-26 | 112.10 | 114.52 | 112.01 | 113.99 | 33721000 | 113.54 |
2014-12-29 | 113.79 | 114.77 | 113.70 | 113.91 | 27598900 | 113.46 |
2014-12-30 | 113.64 | 113.92 | 112.11 | 112.52 | 29881500 | 112.08 |
2014-12-31 | 112.82 | 113.13 | 110.21 | 110.38 | 41403400 | 109.95 |
2015-01-02 | 111.39 | 111.44 | 107.35 | 109.33 | 53204600 | 108.90 |
df.index
<class 'pandas.tseries.index.DatetimeIndex'> [2006-10-02, ..., 2015-01-02] Length: 2078, Freq: None, Timezone: None
df['diff'] = df.Open - df.Close
df.head()
Open | High | Low | Close | Volume | Adj Close | diff | |
---|---|---|---|---|---|---|---|
Date | |||||||
2006-10-02 | 75.10 | 75.87 | 74.30 | 74.86 | 178159800 | 10.09 | 0.24 |
2006-10-03 | 74.45 | 74.95 | 73.19 | 74.08 | 197677200 | 9.98 | 0.37 |
2006-10-04 | 74.10 | 75.46 | 73.16 | 75.38 | 207270700 | 10.16 | -1.28 |
2006-10-05 | 74.53 | 76.16 | 74.13 | 74.83 | 170970800 | 10.08 | -0.30 |
2006-10-06 | 74.42 | 75.04 | 73.81 | 74.22 | 116739700 | 10.00 | 0.20 |
We can have some interacitve elements in the notebook.
def show_col(dataframe, name):
ts = dataframe[name][-10:]
print ts
from IPython.html.widgets import interact, fixed
interact(show_col, dataframe=fixed(df), name=list(df.columns.values))
Date 2014-12-18 111.87 2014-12-19 112.26 2014-12-22 112.16 2014-12-23 113.23 2014-12-24 112.58 2014-12-26 112.10 2014-12-29 113.79 2014-12-30 113.64 2014-12-31 112.82 2015-01-02 111.39 Name: Open, dtype: float64
<function __main__.show_col>
close_px = df['Adj Close']
mavg = pd.rolling_mean(close_px, 40)
%pylab inline
close_px.plot(label='AAPL')
mavg.plot(label='mavg')
plt.legend(loc=4)
Populating the interactive namespace from numpy and matplotlib
<matplotlib.legend.Legend at 0x110a7c950>