Getting Data from the Web: Using APIs for Data Science (JSON to Pandas)

3D isometric illustration of JSON data blocks from an APIs for Data Science cloud being converted into a Pandas DataFrame grid.

In our Pandas Guide, we loaded data from CSV files. But modern data often lives on the web, accessible via APIs. For data scientists, understanding APIs for data science can greatly expand the reach and impact of their projects.

Data Scientists need to know how to grab this live data and get it into a DataFrame quickly.

Step 1: Fetch the Data (Requests)

We’ll use a free, public API that doesn’t need a key: https://api.coindesk.com/v1/bpi/currentprice.json (Bitcoin Price Index).

import requests
import pandas as pd

url = "https://api.coindesk.com/v1/bpi/currentprice.json"
response = requests.get(url)
data = response.json()

Step 2: Inspect the JSON

Before we can make a DataFrame, we need to see the structure.

print(data)
# You'll see it's a nested dictionary. The real data is under the 'bpi' key.

Step 3: Convert to DataFrame

Pandas has a magic function called pd.json_normalize that can flatten nested JSON, but sometimes it’s easier just to grab the specific part you need.

# The 'bpi' key holds the currency data we want
bpi_data = data['bpi']

# Convert that dictionary directly into a DataFrame
df = pd.DataFrame(bpi_data)

# Transpose it (flip rows/columns) to make it readable
df = df.transpose()

print(df)

Now you have a clean DataFrame with USD, GBP, and EUR Bitcoin prices that you can analyze or plot!

Similar Posts

Leave a Reply