DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Kubernetes in the Enterprise: Join our Virtual Roundtable as we dive into Kubernetes over the past year, core usages, and emerging trends.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

Getting Started With Jenkins: Learn fundamentals that underpin CI/CD, how to create a pipeline, and when and where to use Jenkins.

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Playing With Pandas DataFrames (With Missing Values Table Example)
  • Simplifying Access to Db2 Databases in Jupyter Notebook

Trending

  • New Profiles Now on DZone!
  • DZone's Article Submission Guidelines
  • How to Submit a Post to DZone
  • Exploring Apache Airflow for Batch Processing Scenario
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Display Currencies in Python's Pandas Data Frames With the Fixer API

How to Display Currencies in Python's Pandas Data Frames With the Fixer API

Let's take a look at a tutorial that explains how to display currencies in Python's Pandas Data Frames with the Fixes API.

Carlos F. Enguix user avatar by
Carlos F. Enguix
·
Updated Oct. 18, 18 · Tutorial
Like (2)
Save
Tweet
Share
13.2K Views

Join the DZone community and get the full member experience.

Join For Free

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.

API Python (language) Data (computing) Pandas

Opinions expressed by DZone contributors are their own.

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Playing With Pandas DataFrames (With Missing Values Table Example)
  • Simplifying Access to Db2 Databases in Jupyter Notebook

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: