0% found this document useful (0 votes)
25 views8 pages

02 - Big - Mac - Inde (5) - JupyterLab

Uploaded by

kevin.epa.gmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

02 - Big - Mac - Inde (5) - JupyterLab

Uploaded by

kevin.epa.gmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2/4/22, 9:09 PM Big_Mac_Index

Big Mac Index


In this activity, you’ll make three API calls to Quandl to find the cost of a Big Mac in three
countries.
Instructions:
1. In the Unsolved folder for this activity, create an environment file ( .env ). Enter your
QUANDL_API_KEY into this file.
2. In the Jupyter notebook, load the environment variables into your environment file by using
the load_dotenv() function.
3. Get the Quandl API key from the environment variables, and store it in a Python variable
named quandl_api_key . Then confirm that you successfully imported the API key by
checking its type with the type function.
4. Create two variables named start_date and end_date to define the period that you
want to get the Big Mac index for. For this activity, you can set both dates to 2020-07-31 .
5. Review the endpoint URLs that the starter notebook supplies. You’ll use these URLs to
access the endpoints for the Big Mac index from three countries: the United States,
Canada, and Mexico.
Hint The Big Mac Index database from Quandl includes data from different
countries. Each country's dataset has a Quandl code with the following format:
ECONOMIST/BIGMAC_{COUNTRY}. This activity uses USA, CAN, and MEX for the
country codes.
1. Make the authenticated API request for each country by using the request.get function.
Then print the response data by using the json.dump function. Analyze the data. Note
that all the countries have the same output structure.
2. Review the output from the Big Mac Index for the United States. Identify the key-value pairs
that define the response object. Determine how to get the dollar_price and
dollar_ppp data elements. Use this information to create a DataFrame with the data of
these indicators for the United States, Canada, and Mexico. The following image shows how
your DataFrame should appear:
3. Use the DataFrame to create a bar chart that compares the dollar_price and the
dollar_ppp of the United States, Canada, and Mexico.

Reference:
Python requests function
json.dumps function
localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 1/8
2/4/22, 9:09 PM Big_Mac_Index

Quandl Big Mac Index Docs


Quandl Big Mac Index Usage
Import all required libraries and dependencies
In [1]:
# Import required libraries and dependencies

import pandas as pd
import os
import json
import requests
from dotenv import load_dotenv
%matplotlib inline

Step 1: In the Unsolved folder for this activity, create an


environment file ( .env ). Enter your QUANDL_API_KEY
into this file.
Step 2: load the environment variables into your
environment file by using the load_dotenv() function.
In [2]:
# Load .env enviroment variables into the notebook
load_dotenv()

True
Out[2]:

Step 3: Get the Quandl API key from the environment


variables, and store it in a Python variable named
quandl_api_key . Then confirm that you successfully
imported the API key by checking its type with the type
function.
In [3]:
# Get the API key from the environment variable and store as
# Python variable
quandl_api_key = os.getenv("QUANDL_API_KEY")

# Using the type funcion, confirm that the Quandl API key is
# available for use in the notebook
type(quandl_api_key)

str
Out[3]:

Step 4: Create two variables named start_date and


localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 2/8
to define the period that you want to get the
2/4/22, 9:09 PM Big_Mac_Index

end_date
Big Mac index for. For this activity, you can set both dates
to 2020-07-31 .
In [4]:
# Create `start_date` and `end_date` variables
# Both should hold the same date of `2020-07-31`
start_date = '2020-07-31'
end_date = '2020-07-31'

Step 5:
notebookReview the
supplies. endpoint
You’ll useURLs
thesethat
URLsthe
tostarter
access the
endpoints for the Big Mac index from
United States, Canada, and Mexico. three countries: the
In [5]:
big_mac_usa = "https://www.quandl.com/api/v3/datasets/ECONOMIST/BIGMAC_USA?start

big_mac_can = "https://www.quandl.com/api/v3/datasets/ECONOMIST/BIGMAC_CAN?start

big_mac_mex = "https://www.quandl.com/api/v3/datasets/ECONOMIST/BIGMAC_MEX?start

big_mac_usa = "https://www.quandl.com/api/v3/datasets/ECONOMIST/ BIGMAC_USA?start_date=" + start_date


+ "&end_date=" + end_date + "&api_key=" + quandl_api_key big_mac_can =
"https://www.quandl.com/api/v3/datasets/ECONOMIST/ BIGMAC_CAN?start_date=" + start_date +
"&end_date=" + end_date + "&api_key=" + quandl_api_key big_mac_mex =
"https://www.quandl.com/api/v3/datasets/ECONOMIST/ BIGMAC_MEX?start_date=" + start_date +
"&end_date=" + end_date + "&api_key=" + quandl_api_key
Step 6: Make the authenticated API request for each
country by using the request.get function. Then print
the response data by using the json.dumps function.
Analyze the data. Note
same output structure. that all the countries have the
In [6]:
# Make the API call for the USD Big Mac Index information
# using requests.get
response_usa= requests.get(big_mac_usa).json()

# Print the response data using the json.dumps function.


print(json.dumps(response_usa, indent=2, sort_keys=True))

{
"dataset": {
"collapse": null,
"column_index": null,
"column_names": [
"Date",
"local_price",
"dollar_ex",
localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 3/8
2/4/22, 9:09 PM Big_Mac_Index
"dollar_price",
"dollar_ppp",
"dollar_valuation",
"dollar_adj_valuation",
"euro_adj_valuation",
"sterling_adj_valuation",
"yen_adj_valuation",
"yuan_adj_valuation"
],
"data": [
[
"2020-07-31",
5.71,
1.0,
5.71,
1.0,
0.0,
0.0,
-1.9,
12.2,
27.900000000000002,
7.000000000000001
]
],
"database_code": "ECONOMIST",
"database_id": 5330,
"dataset_code": "BIGMAC_USA",
"description": "The Big Mac Index is an informal measure of currency exchang
e rates at ppp. It measures their value against a similar basket of goods and se
rvices, in this case a Big Mac. Differing prices at market exchange rates would
imply that one currency is under or overvalued.",
"end_date": "2020-07-31",
"frequency": "daily",
"id": 9776656,
"limit": null,
"name": "Big Mac Index - United States",
"newest_available_date": "2020-07-31",
"oldest_available_date": "2000-04-30",
"order": null,
"premium": false,
"refreshed_at": "2020-11-15T00:00:29.042Z",
"start_date": "2020-07-31",
"transform": null,
"type": "Time Series"
}
}

In [7]:
# Make the API call for the CAN Big Mac Index information
# using requests.get
response_can = requests.get(big_mac_can).json()

# Print the response data using the json.dumps function.


print(json.dumps(response_can, indent=2, sort_keys=True))

{
"dataset": {
"collapse": null,
"column_index": null,
"column_names": [

localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 4/8
2/4/22, 9:09 PM Big_Mac_Index
"Date",
"local_price",
"dollar_ex",
"dollar_price",
"dollar_ppp",
"dollar_valuation",
"dollar_adj_valuation",
"euro_adj_valuation",
"sterling_adj_valuation",
"yen_adj_valuation",
"yuan_adj_valuation"
],
"data": [
[
"2020-07-31",
6.88,
1.3552,
5.0767414403778,
1.2049036777583177,
-11.09,
2.3,
0.3,
14.7,
30.8,
9.4
]
],
"database_code": "ECONOMIST",
"database_id": 5330,
"dataset_code": "BIGMAC_CAN",
"description": "The Big Mac Index is an informal measure of currency exchang
e rates at ppp. It measures their value against a similar basket of goods and se
rvices, in this case a Big Mac. Differing prices at market exchange rates would
imply that one currency is under or overvalued.",
"end_date": "2020-07-31",
"frequency": "daily",
"id": 9776618,
"limit": null,
"name": "Big Mac Index - Canada",
"newest_available_date": "2020-07-31",
"oldest_available_date": "2000-04-30",
"order": null,
"premium": false,
"refreshed_at": "2020-11-15T00:00:29.042Z",
"start_date": "2020-07-31",
"transform": null,
"type": "Time Series"
}
}

In [8]:
# Make the API call for the MEX Big Mac Index information
# using requests.get
response_mex = requests.get(big_mac_mex).json()

# Print the response data using the json.dumps function.


print(json.dumps(response_mex, indent=2, sort_keys=True))

localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 5/8
2/4/22, 9:09 PM Big_Mac_Index
"dataset": {
"collapse": null,
"column_index": null,
"column_names": [
"Date",
"local_price",
"dollar_ex",
"dollar_price",
"dollar_ppp",
"dollar_valuation",
"dollar_adj_valuation",
"euro_adj_valuation",
"sterling_adj_valuation",
"yen_adj_valuation",
"yuan_adj_valuation"
],
"data": [
[
"2020-07-31",
50.0,
22.436,
2.2285612408629,
8.75656742556918,
-60.971,
-33.0,
-34.300000000000004,
-24.8,
-14.2,
-28.299999999999997
]
],
"database_code": "ECONOMIST",
"database_id": 5330,
"dataset_code": "BIGMAC_MEX",
"description": "The Big Mac Index is an informal measure of currency exchang
e rates at ppp. It measures their value against a similar basket of goods and se
rvices, in this case a Big Mac. Differing prices at market exchange rates would
imply that one currency is under or overvalued.",
"end_date": "2020-07-31",
"frequency": "daily",
"id": 9776636,
"limit": null,
"name": "Big Mac Index - Mexico",
"newest_available_date": "2020-07-31",
"oldest_available_date": "2000-04-30",
"order": null,
"premium": false,
"refreshed_at": "2020-11-15T00:00:29.042Z",
"start_date": "2020-07-31",
"transform": null,
"type": "Time Series"
}
}

Step 7:
United Review
States. the output
Identify the from the Big
key-value Mac
pairs Index
that for
definethe
the
response object. Determine how to get the
localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 6/8
and dollar_ppp data elements. Use
2/4/22, 9:09 PM Big_Mac_Index

dollar_price
this
theseinformation
indicators to create
for the a DataFrame
United States, with the
Canada, data
and of
Mexico.
In [9]:
# Set the Big Mac Index data to create a DataFrame
big_mac_index_data = {
"dollar_price": [
response_usa["dataset"]["data"][0][3],
response_can["dataset"]["data"][0][3],
response_mex["dataset"]["data"][0][3]
],
"dollar_ppp": [
response_usa["dataset"]["data"][0][4],
response_can["dataset"]["data"][0][4],
response_mex["dataset"]["data"][0][4]
]
}

# Create the Big Mac Index DataFrame


big_mac_index_df = pd.DataFrame(big_mac_index_data,
index=["USA", "Canada", "Mexico"])

# Diplay the DataFrame data


big_mac_index_df

Out[9]: dollar_price dollar_ppp


USA 5.710000 1.000000
Canada 5.076741 1.204904
Mexico 2.228561 8.756567

Step 8: Use the DataFrame to create a bar chart that compares the
dollar_price and the dollar_ppp of the United States,
Canada, and Mexico.
In [11]:
# Create a bar chart to analyze the Big Mac Index for the United States,
# Canada and Mexico
# Be sure to include a title and adjust the figure size.
big_mac_index_df.plot.bar(figsize=(10,7),
title="Big Mac Index - USA, Canada & Mexico")

<AxesSubplot:title={'center':'Big Mac Index - USA, Canada & Mexico'}>


Out[11]:

localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 7/8
2/4/22, 9:09 PM Big_Mac_Index

In [ ]:

localhost:8888/lab/tree/02_Big_Mac_Index/Solved/Big_Mac_Index.ipynb 8/8

You might also like