Day 13 - Fetching Data from an API and Converting to a DataFrame in Python
Nischal Baidar
1 min read
APIs provide dynamic data that can be easily fetched and transformed into a pandas DataFrame for analysis. Here's a simple guide on how to do it.
1. Fetching Data from an API
Use the requests
library to get data from an API.
import requests
response = requests.get('https://jsonplaceholder.typicode.com/users')
data = response.json() if response.status_code == 200 else None
2. Converting API Data to a DataFrame
With the JSON data fetched, use pandas to convert it into a DataFrame.
import pandas as pd
df = pd.DataFrame(data)
print(df)
Complete Example:
import requests
import pandas as pd
response = requests.get('https://jsonplaceholder.typicode.com/users')
data = response.json()
df = pd.DataFrame(data)
print(df[['name', 'email']])
This fetches API data and converts it into a DataFrame, making it easy to analyze or manipulate.
0
Subscribe to my newsletter
Read articles from Nischal Baidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by