Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GraceLoggins/09ebfc0e4021c1a7aff9efe53a922f65 to your computer and use it in GitHub Desktop.
Save GraceLoggins/09ebfc0e4021c1a7aff9efe53a922f65 to your computer and use it in GitHub Desktop.
Coursera Python Project APIs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://gitlab.com/ibm/skills-network/courses/placeholder101/-/raw/master/labs/module%201/images/IDSNlogo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Peer Review Assignment - Data Engineer - Extract API Data\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Estimated time needed: **20** minutes\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Objectives\n",
"\n",
"In this part you will:\n",
"\n",
"* Collect exchange rate data using an API\n",
"* Store the data as a CSV\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this lab, we are going to be using Python and several Python libraries. Some of these libraries might be installed in your lab environment or in SN Labs. Others may need to be installed by you. The cells below will install these libraries when executed.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"#!pip install pandas\n",
"#!pip install requests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports\n",
"\n",
"Import any additional libraries you may need here.\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extract Data Using an API\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using ExchangeRate-API we will extract currency exchange rate data. Use the below steps to get the access key and to get the data.\n",
"\n",
"1. Open the url : [https://exchangeratesapi.io/](https://exchangeratesapi.io/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0221ENSkillsNetwork23455645-2021-01-01) and create a free account.\n",
"2. Once the account is created. You will get the Get the Free API key option on the top as shown below:\n",
"\n",
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0221EN-SkillsNetwork/labs/module%206/Final%20Assignment/Images/getapi.png\"/>\n",
"\n",
"3. Copy the API key and use in the url in Question 1.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Call the API\n",
"\n",
"<b> Question 1</b> Using the `requests` library call the endpoint given above and save the text, remember the first few characters of the output:\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"success\":true,\"timestamp\":1641526144,\"base\":\"EUR\",\"date\":\"2022-01-07\",\"rates\":{\"AED\":4.150886,\"AFN\":118.556599,\"ALL\":121.375981,\"AMD\":546.42883,\"ANG\":2.038832,\"AOA\":623.463731,\"ARS\":116.656896,'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code here\n",
"url = \"http://api.exchangeratesapi.io/v1/latest?base=EUR&access_key=9beb24d07d7f852523823821d7414285\" #Make sure to change ******* to your API key.\n",
"page = requests.get(url).text\n",
"page[0:195]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save as DataFrame\n",
"\n",
"<b> Question 2</b> Using the data gathered turn it into a `pandas` dataframe. The dataframe should have the Currency as the index and `Rate` as their columns. Make sure to drop unnecessary columns.\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Currencies</th>\n",
" <th>Rates</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>AED</th>\n",
" <td>AED</td>\n",
" <td>4.150886</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AFN</th>\n",
" <td>AFN</td>\n",
" <td>118.556599</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ALL</th>\n",
" <td>ALL</td>\n",
" <td>121.375981</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AMD</th>\n",
" <td>AMD</td>\n",
" <td>546.42883</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ANG</th>\n",
" <td>ANG</td>\n",
" <td>2.038832</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>YER</th>\n",
" <td>YER</td>\n",
" <td>282.519695</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZAR</th>\n",
" <td>ZAR</td>\n",
" <td>17.768645</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZMK</th>\n",
" <td>ZMK</td>\n",
" <td>10172.053776</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZMW</th>\n",
" <td>ZMW</td>\n",
" <td>18.977707</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZWL</th>\n",
" <td>ZWL</td>\n",
" <td>363.88452</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>168 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" Currencies Rates\n",
"AED AED 4.150886\n",
"AFN AFN 118.556599\n",
"ALL ALL 121.375981\n",
"AMD AMD 546.42883\n",
"ANG ANG 2.038832\n",
".. ... ...\n",
"YER YER 282.519695\n",
"ZAR ZAR 17.768645\n",
"ZMK ZMK 10172.053776\n",
"ZMW ZMW 18.977707\n",
"ZWL ZWL 363.88452\n",
"\n",
"[168 rows x 2 columns]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Turn the data into a dataframe\n",
"rateSub = page[(page.find('rates'))+8:-2].strip()\n",
"listing = rateSub.split(',')\n",
"currenciesList = []\n",
"ratesList = []\n",
"\n",
"for i in listing:\n",
" i=i.replace('\"', '').strip('{}')\n",
" currenciesList.append(str(i[0:3]))\n",
" ratesList.append(i[4::])\n",
"\n",
"dic = {'Currencies':currenciesList, 'Rates':ratesList}\n",
"df = pd.DataFrame(data=dic, index=currenciesList)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Rates</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>AED</th>\n",
" <td>4.150886</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AFN</th>\n",
" <td>118.556599</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ALL</th>\n",
" <td>121.375981</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AMD</th>\n",
" <td>546.42883</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ANG</th>\n",
" <td>2.038832</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>YER</th>\n",
" <td>282.519695</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZAR</th>\n",
" <td>17.768645</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZMK</th>\n",
" <td>10172.053776</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZMW</th>\n",
" <td>18.977707</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ZWL</th>\n",
" <td>363.88452</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>168 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" Rates\n",
"AED 4.150886\n",
"AFN 118.556599\n",
"ALL 121.375981\n",
"AMD 546.42883\n",
"ANG 2.038832\n",
".. ...\n",
"YER 282.519695\n",
"ZAR 17.768645\n",
"ZMK 10172.053776\n",
"ZMW 18.977707\n",
"ZWL 363.88452\n",
"\n",
"[168 rows x 1 columns]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Drop unnescessary columns\n",
"df = df.drop(columns=['Currencies'])\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load the Data\n",
"\n",
"Using the dataframe save it as a CSV names `exchange_rates_1.csv`.\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Save the Dataframe\n",
"df.to_csv('exchange_rates"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Your CSV should be in this format with more currencies\n",
"\n",
"| | Rates |\n",
"| --- | ---------- |\n",
"| AED | 4.398618 |\n",
"| AFN | 92.917693 |\n",
"| ALL | 123.099093 |\n",
"| AMD | 621.935674 |\n",
"| ANG | 2.149648 |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Authors\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ramesh Sannareddy, Joseph Santarcangelo and Azim Hirjani\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Other Contributors\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rav Ahuja\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change Log\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ----------------- | ---------------------------------- |\n",
"| 2021-04-15 | 0.2 | Malika | Updated the lab from USD to EUR |\n",
"| 2020-11-25 | 0.1 | Ramesh Sannareddy | Created initial version of the lab |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright © 2020 IBM Corporation. This notebook and its source code are released under the terms of the [MIT License](https://cognitiveclass.ai/mit-license?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0221ENSkillsNetwork23455645-2021-01-01&cm_mmc=Email_Newsletter-\\_-Developer_Ed%2BTech-\\_-WW_WW-\\_-SkillsNetwork-Courses-IBM-DA0321EN-SkillsNetwork-21426264&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ).\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment