
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!
Key Takeaways
- APIs for Data Science allow data scientists to access live data from the web.
- To fetch data, use a free public API like the Bitcoin Price Index for quick access.
- Inspect the JSON structure before creating a DataFrame to ensure proper data handling.
- Utilise Pandas’ `pd.json_normalize` to flatten nested JSON or select specific data more easily.
- Create a clean DataFrame with Bitcoin prices in multiple currencies for analysis or visualisation.





