Practice assignment 2
Practice assignment 2
Objective: In this assignment, you will practice working with JSON data from a free API, from
https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=7
extracting data into a list of dictionaries, converting JSON data to a Pandas DataFrame using
json_normalize, and using Python functions like enumerate
Print the top-level keys of the JSON response to understand its structure (use
data.keys() )
Investigate the structure of the JSON response in an online JSON viewer like
https://jsonviewer.stack.hu/ . You will need to know how it looks like in order to be able to
process this data. You can open the API link in a browser to get the ‘raw’ data as well (may be
easier to copy-past from the browser compared to the Jupyter notebook cell).
2. List of Dictionaries
When you are looping through a list, it may be helpful to use enumerate. Enumerate will give
you a tuple, of each list element as well as a counter.
Extract the prices, market_caps, and total_volumes data from the JSON response (each
of these will be a list).
Create a list of dictionaries where each dictionary represents a data point and contains:
Timestamp
Price
Market Cap
Volume
Add a unique identifier (ID) to each dictionary (0, 1, 2, …)
3. Data Transformation
Add a column Daily Change to calculate the difference in prices compared to the
previous day.
Initialize the first data point's daily change as 0 and use a for loop to calculate the daily
change for subsequent data points.
Add a column Daily Percentage Change to calculate the percentage change in price:
Daily Percentage Change = ((Current Price - Previous Price) / Previous Price) * 100
4. JSON to Pandas DataFrame
Convert the list of dictionaries to a Pandas DataFrame.
Display the first 5 rows of the DataFrame to verify the data structure.
Display minimum, average and maximum values for Price, Daily Change and Daily
Percentage Change.
Identify the dates with the highest and lowest prices; for example
df["price"].max() will give the highest price, and df["price"].idxmax()
will give the index of the observation with the highest price
6. Export