REST API to Pandas
I'm a complete beginner trying to learn how to use APIs to pull data then analyze that data. I was able to send a request to receive a csv which returned a 200 status code and prints correctly, but I can't seem to read it into a pandas DF. What am I doing wrong?
request = requests.get(host + endpoint + key, params={'format':'csv'})
df = pd.read_csv(request.text, sep=',')
Error message is ValueError: Protocol not known: contract_decimals,contract_name,contract_ticker_symbol,contract_address,logo_url,last_transferred_at,type,balance,balance_24h,quote_rate,quote_rate_24h,quote,quote_24h,nft_data,supports_erc_0,supports_erc 18,"akSwap.io","akSwap.io","0x82dfdb2ec1aa6003ed4acba663403d7c2127ff67","https
I'd love to know what I'm doing wrong but also would love to know where I can go to learn more about using API data from pandas to construct a dashboard (something a little more structured than the docs would be great!)
Solution 1:
You're using the pd.read_csv
function incorrectly.
Looking at the docs...
pandas.read_csv(filepath_or_buffer, sep=NoDefault.no_default, ...
Parameters filepath_or_bufferstr, path object or file-like object Any valid string path is acceptable
The first argument of the read_csv function accepts a filepath or buffer. You are passing it a string object that you obtained from the api response request.text
.
Try this instead. Inspired from this StackOverflow thread
from io import StringIO
request = requests.get(host + endpoint + key, params={'format':'csv'})
df = pd.read_csv(StringIO(request.text), sep=',')
You can refer to this documentation on the requests
library you're using.
Depending on what API you're using, you should be easily able to coerce the API response into a pandas DataFrame using the standard functions available in pandas.
- read_csv
- read_json
The pandas documentation is pretty comprehensive and has good Getting Started docs too.