Start by installing locally a comprehensive Python installation such as the Anaconda Python distribution.
Import the required modules/packages
In [207]:
import numpy as np # for array operations
import pandas as pd # for time series management
from pandas_datareader import data as web # for data retrieval
import seaborn as sns; sns.set() # for a nicer plotting style
# put all plots in the notebook itself
%matplotlib inline
In this case, I'm retrieving stock price data for American Express Company using its stock symbol AXP from Google Finance.
In [208]:
AXP = web.DataReader('AXP', data_source='google')
In [209]:
type(AXP)
Out[209]:
Get meta information
In [210]:
AXP.info()
List the columns in the dataframe
In [211]:
AXP.columns
Out[211]:
Display the final five rows of the data set.
In [212]:
AXP.tail()
Out[212]:
Easily select single or multiple columns of a DataFrame object.
.head() shows the first five rows of the selected column
In [213]:
AXP['Close'].head()
Out[213]:
.tail() here, shows the last five rows of the 2 selected columns
In [214]:
AXP[['Open', 'Close']].tail()
Out[214]:
Similarly, a single or multiple rows can be selected
In [215]:
AXP.loc['2017-06-05'] # single row via index value
Out[215]:
In [216]:
AXP.iloc[:2] # two rows via index numbers
Out[216]:
In [217]:
AXP['Close'].plot(figsize=(20, 10));
fully vectorized operation for log return calculation
In [218]:
rets = np.log(AXP['Close'] / AXP['Close'].shift(1))
The log returns can then be visualized via a histogram.
In [219]:
rets.hist(figsize=(20, 10), bins=35);
vectorized calculation of 50 days moving average/trend
In [220]:
AXP['MA50'] = pd.Series(AXP['Close']).rolling(window=50,center=False).mean()
In [221]:
AXP[['Close', 'MA50']].plot(figsize=(20, 10));