import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.float_format = '{:.4f}'.format
plt.style.use("seaborn")
close = pd.read_csv("close.csv", index_col = "Date", parse_dates = ["Date"])
close
Drop the NaN value without changing the original data frame
msft = close.MSFT.dropna().to_frame().copy()
Change a column label MSFT to Price:
msft.rename(columns = {"MSFT":"Price"}, inplace = True)
Last day of price:
msft["P_lag1"] = msft.shift(periods = 1)
Compare between two days of price:
msft["P_diff"] = msft.Price.sub(msft.P_lag1) # Alternative 1
msft["P_diff2"] = msft.Price.diff(periods = 1) # Alternative 2
#print msft table
msft
Absolute price changes are not meaningful yet, this is how we can calculate relative changes
#percentage change method
msft["Returns"] = msft.Price.pct_change(periods = 1) * 100 # Alternative 2
msft
The output of percentage change:
save the file to csv format:
msft.drop(columns = ["P_lag1", "P_diff", "P_diff2"], inplace = True)
msft.to_csv("msft.csv")