How do you call a REST API, gather the response, and then read a CSV into a pandas DataFrame?
1Times asked
Jun 2026Last seen
Jun 2026First seen
💡 Model Answer
To call a REST API in Python you typically use the requests library. First, send a GET or POST request and check the status code. Then parse the JSON response with response.json(). If the API returns a CSV file or a URL to a CSV, you can read it into a pandas DataFrame using pd.read_csv. Example:
python
import requests
import pandas as pd
# Call the API
url = "https://api.example.com/data"
headers = {"Authorization": "Bearer YOUR_TOKEN"}
resp = requests.get(url, headers=headers)
resp.raise_for_status() # raise an error for bad status
# If the API returns JSON that contains a CSV URL
csv_url = resp.json().get("csv_url")
# Or if the API returns CSV content directly
# csv_content = resp.text
# Read CSV into DataFrame
if csv_url:
df = pd.read_csv(csv_url)
else:
df = pd.read_csv(pd.compat.StringIO(resp.text))
print(df.head())Complexity: The request is O(1) network latency; reading the CSV is O(n) where n is the number of rows. The code handles errors and can be extended to stream large files.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500