I'm very new to coding, but am really enjoying messing around with pulling data off Yahoo finance. I've so far come up with code that pulls the close price off for select tickers, and calculates the monthly % change - displaying it as a transposed table with 2 columns. When returning the data in a dataframe, I want to be able to name both columns using .columns, but it doesn't seem to recognise the left hand column at all (the one with the tickers in). When I try to pass more than 1 item to .column, it gives me an error saying there's only 1 column. Does anyone know why this would be, and how I could manipulate the data to have the left hand column recognised?
symbols_list = ["AAPL", "TSLA"]
d = {}
for x in symbols_list:
try:
d[x] = web.DataReader(x, "yahoo", '2015-12-31')
except:
print "Can't find ", x
ticker = pd.Panel(d)
df1 = ticker.minor_xs('Adj Close')
daily = df1.resample('B', how=lambda x: x)
monthly_change = daily.pct_change(periods = 24)
past_month_change = monthly_change.tail(1)
transposed = past_month_change.transpose()
transposed.columns = ["Monthly % Change"] #This works, but only names the right column, not the left.
after executing your code i have the following:
In [317]: transposed.columns
Out[317]: DatetimeIndex(['2016-06-03'], dtype='datetime64[ns]', name='Date', freq='B')
In [318]: transposed.info()
<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, AAPL to TSLA
Data columns (total 1 columns):
2016-06-03 00:00:00 2 non-null object
dtypes: object(1)
memory usage: 32.0+ bytes
now you can reset index and rename columns:
In [320]: transposed = transposed.reset_index()
In [321]: transposed.columns = ['code', 'Monthly % Change']
In [322]: transposed
Out[322]:
code Monthly % Change
0 AAPL 0.0520737
1 TSLA -0.0943342