I'm reading tables from the web using pd.read_html and using a for loop in pandas to create a single dataframe from multiple dataframes. I'm able to successfully create a data frame for any given year but I'm missing the correct logic in the for loop to:
(1) Read data, (2) create a dataframe (3) Go to the next year and (4) Append that dataframe to previous dataframe.
The ideal outcome should be 1 dataframe with ~500 rows and 13 columns (for 2 years worth of data). Thanks!
for x in range(2017, 2019):
dfs = pd.read_html('https://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/TextView.aspx?data=yieldYear&year=(%d)' % x, skiprows=1)
for df in dfs:
print df
DateList = ['Date', '1 mo', '2 mo', '3 mo', '6 mo', '1 yr', '2 yr', '3 yr', '5 yr', '7 yr', '10 yr', '20 yr', '30 yr']
df.columns = DateList