Hello and welcome to part 2 of the Python for Finance tutorial series. In this tutorial, we're going to further break down some basic data manipulation and visualizations with our stock data. The starting code that we're going to be using (which was covered in the previous tutorial) is:
import datetime as dt import matplotlib.pyplot as plt from matplotlib import style import pandas as pd import pandas_datareader.data as web style.use('ggplot') start = dt.datetime(2015, 1, 1) end = dt.datetime.now() df = web.DataReader("TSLA", 'morningstar', start, end) df.reset_index(inplace=True) df.set_index("Date", inplace=True) df = df.drop("Symbol", axis=1)
What are some things we can do with these DataFrames? For one, we can save them easily to a variety of datatypes. One option is a csv:
Rather than reading data from Yahoo's finance API to a DataFrame, we can also read data from a CSV file into a DataFrame:
df = pd.read_csv('tsla.csv', parse_dates=True, index_col=0)
Now, we can graph with:
Cool, except that the only thing we can really see here is the volume, since it's on a scale MUCH larger than stock price. How can we maybe just graph what we're interested in?
df['Adj Close'].plot() plt.show()
As you can see, you can reference specific columns in the DataFrame like:
df['Adj Close'], but you can also reference multiple at a time, like so:
In the next tutorial, we're going to cover some basic manipulations of this data, along with some more basic visualizations.