1

I am trying to convert a json payload with no keys into a Pandas Dataframe. Below is what I have now

import requests
import pandas as pd

r = requests.get("https://api.pro.coinbase.com/products/BTC-USD/candles")

df = pd.read_json(r.json()).columns = ["time", "low", "high", "open", "close", "volume"]

print(df)

The request returns a payload like this...

[
    [
        1613256660,
        47077.61,
        47103.32,
        47100,
        47084.96,
        1.6661533
    ],
    ...
]
1
  • 2
    Your code is correct but for some syntax errors - pd.DataFrame(r.json(), columns = ["time", "low", "high", "open", "close", "volume"]) will work Commented Feb 14, 2021 at 0:04

1 Answer 1

2

pd.read_json is used to read JSON string data. In your case, requests.Response has already parsed the JSON and is returning a list of lists when you call response.json(). So it should be sufficient to pass the list structure to pd.DataFrame directly:

df = pd.DataFrame(response.json(), 
                  columns=["time", "low", "high", "open", "close", "volume"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.