How to Display Currencies in Python's Pandas Data Frames With the Fixer API
Due to globalization we might need to know the value of a given currency and compare it to others when buying goods at Amazon, or any online shop. In some cases, some of us trade in different currencies for buying houses, or any goods in foreign countries or just speculate with the value of Dollars with respect to the Euro for instance to obtain revenues.
An Introduction to the Fixer API
The fixer API allows us to create programs to obtain in a summarised way the value of different currencies compared to others. There is a python API to interact with the fixer API.
There are different plans in the fixer Web site. As a proof-of-concept, we can start with the free plan, which allows us the following:
1.000 API Calls Per Month
Hourly Updates
Limited Support
Historical Data
Which Subscription Should We Get?
Currently, there are two subscriptions with regard to defining base currencies. I have subscribed to the legacy plan which allows me to indicate different base currencies as compared to the latest that does not allow it in the free plan.
How Do I Subscribe?
Pretty easy, instead of just going to the fixer API site: https://fixer.io/ and sign up for a free access key (with the restriction that it allows only the Euro as a base currency), sign up for the legacy API key at https://fixer.io/signup/legacy. Now you are good to go, with respect to defining different base currencies. Remember to save your access key and your details so you are able to access your dashboard and include in your python code the access key.
First Test the API Call in Your Web Browser
You might say that the code is not very pythonic, but please be assured that the major objective was to keep the code as simple as possible. First of all, you should try to confirm that your legacy access key is working fine by inserting the whole URL in your favorite Web browser, in my case I use Google Chrome:
http://data.fixer.io/api/latest?access_key=yourkey&base=USD&symbols=EUR,PEN,AUD
If everything is working fine you should receive as output in your Web browser a JSON document like the following:
{
"success": true,
"timestamp": 1539195845,
"base": "USD",
"date": "2018-10-10",
"rates": {
"EUR": 0.866739,
"PEN": 3.32985,
"AUD": 1.40915
}
}
Let’s Code in Python
Now that we tested that the URL was working fine by returning a JSON document we are able to code in python (code is simple and self-explanatory):
import requests
import json
import datetime
import pandas as pd
# legacy api call
# example of url="http://data.fixer.io/api/latest?access_key=your_legacy_access_key&base=USD&symbols=EUR,PEN,AUD"
def get_pd_old_fixer_api(access_key, base, symbols):
# generate comma separated list of symbol currencies
str_symbols = ",".join(str(x) for x in symbols)
# generate url with access key, base currency, and list of comma separated currencies to be converted
url = "http://data.fixer.io/api/latest?access_key=" + access_key + "&base=" + base + "&symbols=" + str_symbols
# send the http request
response = requests.get(url)
# retrieve the json output
data = response.text
parsed = json.loads(data)
# generate the list of currencies values
data = []
for symbol in symbols:
if symbol != base:
data.append(parsed["rates"][symbol])
else:
# symbol coincides with base currency
data.append("1")
cols = {base}
# create the pandas data frame for this base currency, and values of the converted currencies
df = pd.DataFrame(data=data, columns=cols, index=symbols)
return df
def main():
access_key = "your_legacy_access_key"
# get_pd_old_fixer_api(access_key, base, symbols)
symbols = ["EUR", "USD", "PEN", "AUD"]
#generate the pandas currencies data frame for each base currency
pd_euro = get_pd_old_fixer_api(access_key, 'EUR', symbols)
pd_usa = get_pd_old_fixer_api(access_key, 'USD', symbols)
pd_pen = get_pd_old_fixer_api(access_key, "PEN", symbols)
pd_aud = get_pd_old_fixer_api(access_key, "AUD", symbols)
date_time = datetime.datetime.now().strftime("%A %d %B %Y %H:%M:%S:%f")
print(f"\nOn {date_time} the currency table is:\n")
pd_total = [pd_euro, pd_usa, pd_pen, pd_aud]
# we concatenate the pandas data frames on the column axis
result = pd.concat(pd_total, axis=1, join="outer", sort=False)
print(result)
if __name__ == '__main__':
main()
And the result of executing this python script is the following for the current time:
EUR USD PEN AUD
EUR 1 0.867945 0.260656 0.613867
USD 1.15215 1 0.300314 0.707265
PEN 3.83648 3.32985 1 2.35508
AUD 1.62902 1.4139 0.424613 1
Process finished with exit code 0
And voilà, as a result, you have a table of currencies and their respective values. Now it’s time for you to adapt the script to your favorite currencies.
I hope that you enjoyed this simple tutorial.