0

I am trying to access the federal reserve bank data at https://fred.stlouisfed.org/series/FEDFUNDS what is the code I can write to access this database and then put it in a dictionary? Or do I have to download the file first and save it on my computer?

2 Answers2

1

The easiest way to pull that data in would be to download and parse the CSV file listed under the "Download" button.

You can use the Requests library to download the file, then use the native CSV library.

Dan S
  • 283
  • 1
  • 12
0

See https://stackoverflow.com/a/32400969/9214517 for how to do it.

Let's say you allow to keep the data in a pandas DataFrame (as the link above do), this is the code:

import pandas as pd
import requests
import io
url = "https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12&tts=12&width=968&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes&show_tooltip=yes&id=FEDFUNDS&scale=left&cosd=1954-07-01&coed=2018-10-01&line_color=%234572a7&link_values=false&line_style=solid&mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Monthly&fam=avg&fgst=lin&fgsnd=2009-06-01&line_index=1&transformation=lin&vintage_date=2018-11-28&revision_date=2018-11-28&nd=1954-07-01"
s = requests.get(url).content.decode("utf-8")
df = pd.read_csv(io.StringIO(s)

Then your df will be:

           DATE  FEDFUNDS
0    1954-07-01      0.80
1    1954-08-01      1.22
2    1954-09-01      1.06
3    1954-10-01      0.85
4    1954-11-01      0.83
....

And if you insist on a dict, use this instead of the last line above to convert your CSV data s:

mydict = dict([line.split(",") for line in s.splitlines()])

The key is how to get the URL: Hit the download button on the page you quoted, and copy the link to CSV.

adrtam
  • 6,991
  • 2
  • 12
  • 27