/python/pandas_ex.py
Python | 37 lines | 22 code | 15 blank | 0 comment | 1 complexity | 8146b5aa42181dca4081f5ffcf46184e MD5 | raw file
- import numpy as np
- import pandas.io.data as web
- def pct_change(x):
- return x / x.shift(1) - 1
- stocks = ['AAPL', 'FB', 'GOOG']
- st = '2012-1-1'
- ed = '2012-9-29'
- dd = {}
- for s in stocks:
- dd[s] = web.get_data_yahoo(s, st, ed)['Adj Close']
- from pandas import DataFrame
- df = DataFrame(dd)
- df
- returns = df.pct_change()
- returns = returns.dropna(how='all').sort_indx(ascending=False)
- type(returns)
- fb = return_arr[:, 1]
- fb.std()
- fb[::-1]
- fb[::2]
- fb_asc = fb[::-1]
- fb_asc = aapl