Home > Enterprise >  API Request to Array
API Request to Array

Time:10-28

I'm using a Crypto API that gives me the Time, Open, High, Low, Close Values of the last weeks back. I just need the first row.

The input:

[[1635260400000, 53744.5, 53744.5, 53430.71, 53430.71], [1635262200000, 53635.49, 53899.73, 53635.49, 53899.73], [1635264000000, 53850.63, 54258.62, 53779.11, 54242.25], [1635265800000, 54264.32, 54264.32, 53909.02, 54003.42]]

I've tried:

resp = pd.read_csv('https://api.coingecko.com/api/v3/coins/bitcoin/ohlc?vs_currency=eur&days=1')
resp = resp.astype(str)


Time = resp[resp.columns[0]]
Open = resp[resp.columns[1]]
High = resp[resp.columns[2]]
Low = resp[resp.columns[3]]
Close = resp[resp.columns[4]]

But this doesn't work as I can't process it(i wanted to process it from object to str to double or float). I want to have each value as a double in a different variable. Im kinda stuck at this.

CodePudding user response:

The problem with using pandas is that the JSON array creates one row with several columns.

If you expect to just loop over the JSON array, I suggest using requests rather than pandas.

import requests

resp = requests.get('https://api.coingecko.com/api/v3/coins/bitcoin/ohlc?vs_currency=eur&days=1')

for row in resp.json():
   timestamp, open_price, high, low, close = row
   ...

CodePudding user response:

You just need to use read_json:

resp = pd.read_json('https://api.coingecko.com/api/v3/coins/bitcoin/ohlc?vs_currency=eur&days=1')
resp = resp.astype(float)

Time = resp[resp.columns[0]]
Open = resp[resp.columns[1]]
High = resp[resp.columns[2]]
Low = resp[resp.columns[3]]
Close = resp[resp.columns[4]]

But the previous solution is more compact and understandable.

  • Related