Python error when trying to loop through stock price closes
Question:
I am trying to loop through stock price csvs in order to add columns such as portfolio weight etc. in order to build a portfolio to analyse in one dataframe. When looping however, I get an error which I do not understand:
gsk_df = web.DataReader("GSK.L", 'yahoo', start=start, end=end)
ocdo_df = web.DataReader("OCDO.L", 'yahoo', start=start, end=end)
rbs_df = web.DataReader("RBS.L", 'yahoo', start=start, end=end)
svt_df = web.DataReader("SVT.L", 'yahoo', start=start, end=end)
iii_df = pd.read_csv("iii (1).csv", parse_dates=['Date'])
#close of each stock
bp_close = bp_df["Adj Close"]
gsk_close = gsk_df["Adj Close"]
ocdo_close = ocdo_df["Adj Close"]
rbs_close = rbs_df["Adj Close"]
svt_close = svt_df["Adj Close"]
iii_close = iii_df["Adj Close"]
#adding normalised returns for portfolio
for stock_df in (bp_close, gsk_close, ocdo_close, rbs_close, svt_close, iii_close):
stock_df['Norm return'] = stock_df['Adj Close']/stock_df.iloc[0]['Adj Close']
#adding portfolio weights
for stock_df, allocation in zip((bp_close, gsk_close, ocdo_close, rbs_close, svt_close, iii_close),[.0881,.233,.160,.0776,.304,.137]):
stock_df['Allocation']=stock_df['Norm return']*allocation
#portfolio position value column
for stock_df in (bp_close, gsk_close, ocdo_close, rbs_close, svt_close, iii_close):
stock_df['Position'] = stock_df['Allocation']*6503800000
print(bp_close.head())
Here is the error:
KeyError: ‘Adj Close’
I had to change the "Adjusted_close" heading to "Adj close" in the iii csv as it was from another data set and the for loop would not function with different column headers. Thanks for the help.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/iFv6N.jpg
Answers:
assuming these series have the stock as their column name, you can avoid the loops
by concatenating them into a dataframe and avoiding the KeyError
stocks = ['GSK.L', 'OCDO.L', 'RBS.L', 'SVT.L']
df = web.DataReader(stocks, 'yahoo',start,end).reset_index(drop=True)
iii_df = pd.read_csv("iii (1).csv", parse_dates=['Date']) # reset_index if needed
stock_df = pd.concat([df, iii_close], axis=1, ignore_index=True)
stock_df['Norm return'] = stock_df / stock_df.loc[0]
stock_df['Allocation'] = stock_df['Norm return'] * [.0881,.233,.160,.0776,.304,.137]
stock_df['Position'] = stock_df['Allocation'] * 6503800000
I am trying to loop through stock price csvs in order to add columns such as portfolio weight etc. in order to build a portfolio to analyse in one dataframe. When looping however, I get an error which I do not understand:
gsk_df = web.DataReader("GSK.L", 'yahoo', start=start, end=end)
ocdo_df = web.DataReader("OCDO.L", 'yahoo', start=start, end=end)
rbs_df = web.DataReader("RBS.L", 'yahoo', start=start, end=end)
svt_df = web.DataReader("SVT.L", 'yahoo', start=start, end=end)
iii_df = pd.read_csv("iii (1).csv", parse_dates=['Date'])
#close of each stock
bp_close = bp_df["Adj Close"]
gsk_close = gsk_df["Adj Close"]
ocdo_close = ocdo_df["Adj Close"]
rbs_close = rbs_df["Adj Close"]
svt_close = svt_df["Adj Close"]
iii_close = iii_df["Adj Close"]
#adding normalised returns for portfolio
for stock_df in (bp_close, gsk_close, ocdo_close, rbs_close, svt_close, iii_close):
stock_df['Norm return'] = stock_df['Adj Close']/stock_df.iloc[0]['Adj Close']
#adding portfolio weights
for stock_df, allocation in zip((bp_close, gsk_close, ocdo_close, rbs_close, svt_close, iii_close),[.0881,.233,.160,.0776,.304,.137]):
stock_df['Allocation']=stock_df['Norm return']*allocation
#portfolio position value column
for stock_df in (bp_close, gsk_close, ocdo_close, rbs_close, svt_close, iii_close):
stock_df['Position'] = stock_df['Allocation']*6503800000
print(bp_close.head())
Here is the error:
KeyError: ‘Adj Close’
I had to change the "Adjusted_close" heading to "Adj close" in the iii csv as it was from another data set and the for loop would not function with different column headers. Thanks for the help.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/iFv6N.jpg
assuming these series have the stock as their column name, you can avoid the loops
by concatenating them into a dataframe and avoiding the KeyError
stocks = ['GSK.L', 'OCDO.L', 'RBS.L', 'SVT.L']
df = web.DataReader(stocks, 'yahoo',start,end).reset_index(drop=True)
iii_df = pd.read_csv("iii (1).csv", parse_dates=['Date']) # reset_index if needed
stock_df = pd.concat([df, iii_close], axis=1, ignore_index=True)
stock_df['Norm return'] = stock_df / stock_df.loc[0]
stock_df['Allocation'] = stock_df['Norm return'] * [.0881,.233,.160,.0776,.304,.137]
stock_df['Position'] = stock_df['Allocation'] * 6503800000