1

This is a multi-part question. I am looking to make a request and print the result in excel, with the headers at the top all across and the content data in the row below. When I run the following code, I only get the headers printed vertically and no data is shown. So my questions are: 1. How do I get the data to come in? and 2. How can I transform the output so the headers are horizontal in the excel sheet (along with the data to come underneath)?

import requests
import json
import pandas
import xlwt

r = requests.get("https://testapp.deribit.com/api/v2/public/get_trade_volumes?")

db=[]

resp = json.loads(r.text)
for data in resp['result'][0]:
    db.append(data)
for data in resp['result'][1]:
    db.append(data)

df1 = pandas.DataFrame(db)
df1.to_excel('C:/Test/daily_volume.xls')

1

1 Answer 1

1

Your for data in resp doesn't work quite like you think it does.

In [26]: db=[] 
    ...:  
    ...: resp = response 
    ...: for data in resp['result'][0]: 
    ...:     db.append(data) 
    ...: for data in resp['result'][1]: 
    ...:     db.append(data) 
    ...:                                                                                                                                                                                                                                       

In [27]: response                                                                                                                                                                                                                              
Out[27]: 
{'jsonrpc': '2.0',
 'result': [{'puts_volume': 1246.1,
   'futures_volume': 1807294.0335,
   'currency_pair': 'btc_usd',
   'calls_volume': 767.4},
  {'puts_volume': 64.0,
   'futures_volume': 130999.2335,
   'currency_pair': 'eth_usd',
   'calls_volume': 79.0}],
 'usIn': 1575569314303219,
 'usOut': 1575569314304869,
 'usDiff': 1650,
 'testnet': True}

In [28]: db                                                                                                                                                                                                                                    
Out[28]: 
['puts_volume',
 'futures_volume',
 'currency_pair',
 'calls_volume',
 'puts_volume',
 'futures_volume',
 'currency_pair',
 'calls_volume']

You don't have to loop through it that way anyway. With the given JSON structure, you can pass that directly into pandas and get what you're after.

import requests
import pandas as pd

response = requests.get("https://testapp.deribit.com/api/v2/public/get_trade_volumes?").json()
pd.DataFrame(response["result"]).to_excel("daily_volume.xls")

Produces

enter image description here

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.