python pandas.read_json()对IG Labs REST不起作用

时间:2018-09-29 15:32:59

标签: json python-3.x pandas

我正在尝试将一些json字符串读入pandas数据帧。我似乎能够检索json本身。但是,当我尝试使用pandas数据框读取数据时,似乎失败了。

任何专家有什么想法吗?

import requests
import json
import pandas

url = 'https://demo-api.ig.com/gateway/deal'
s = requests.Session()
s.headers = { 'Content-Type' : 'application/json; charset=UTF-8',
              'Accept' : 'application/json; charset=UTF-8',
              'VERSION' : '2',
              'X-IG-API-KEY' : 'XXX'
              }
data = { 'identifier' : 'XXX',
         'password' : 'XXX'
         }

r = s.post(url + '/session', json=data)
r.json()

我在下面得到json字符串数据

> {'accountType': 'CFD',  'accountInfo': {'balance': 0,  
> 'deposit': 0.0,   'profitLoss': 0.0,   'available': 0}, 
> 'currencyIsoCode': 'GBP',  'currencySymbol': '£',  'currentAccountId':
> 'XXXX',  'lightstreamerEndpoint':
> 'https://demo-apd.marketdatasystems.com',  'accounts': [{'accountId':
> 'xxx',    'accountName': 'Demo-Spread bet',    'preferred': False,  
> 'accountType': 'SPREADBET'},   {'accountId': 'xxx',   
> 'accountName': 'Demo-CFD',    'preferred': True,    'accountType':
> 'CFD'}],  'clientId': 'xxx',  'timezoneOffset': 1, 
> 'hasActiveDemoAccounts': True,  'hasActiveLiveAccounts': True, 
> 'trailingStopsEnabled': False,  'reroutingEnvironment': None, 
> 'dealingEnabled': True}

但是,当我尝试将r.json()转换为熊猫时,使用data = pandas.read_json(r.json());我收到错误 ValueError:无效的文件路径或缓冲区对象类型:

1 个答案:

答案 0 :(得分:1)

进口:

import pandas as pd

创建主数据框:

df_main = pd.DataFrame()

数据:它不是json格式

data = {'accountType': 'CFD',
        'accountInfo': {'balance': 0,
                        'deposit': 0.0,
                        'profitLoss': 0.0,
                        'available': 0},
        'currencyIsoCode': 'GBP',
        'currencySymbol': '£',
        'currentAccountId': 'XXXX',
        'lightstreamerEndpoint': 'https://demo-apd.marketdatasystems.com',
        'accounts': [{'accountId': 'xxx',
                      'accountName': 'Demo-Spread bet',
                      'preferred': False,
                      'accountType': 'SPREADBET'}, 
                     {'accountId': 'xxx',
                      'accountName': 'Demo-CFD',
                      'preferred': True,
                      'accountType': 'CFD'}],
        'clientId': 'xxx',
        'timezoneOffset': 1, 
        'hasActiveDemoAccounts': True,
        'hasActiveLiveAccounts': True, 
        'trailingStopsEnabled': False,
        'reroutingEnvironment': None, 
        'dealingEnabled': True}

DataFrame.from_dict:每个键都是一个索引

df = pd.DataFrame.from_dict(data, orient='index')

转置它:每个键现在是一个列标题,并且值在行中

df_trans = df.transpose()

将其附加到df_main:每条记录都附加到df_main

df_main = df_main.append(df_tras, ignore_index=True)

请记住在所选答案旁边打勾

这是数据的有效json格式:将其粘贴到文件中并命名为data.json,然后使用pd.read_json('data.json'),您将获得相同的输出为df = pd.DataFrame.from_dict(data, orient='index')

{
   "data":{
      "accountType":"CFD",
      "accountInfo":{
         "balance":0,
         "deposit":0.0,
         "profitLoss":0.0,
         "available":0
      },
      "currencyIsoCode":"GBP",
      "currencySymbol":"£",
      "currentAccountId":"XXXX",
      "lightstreamerEndpoint":"https://demo-apd.marketdatasystems.com",
      "accounts":[
         {
            "accountId":"xxx",
            "accountName":"Demo-Spread bet",
            "preferred":"False",
            "accountType":"SPREADBET"
         },
         {
            "accountId":"xxx",
            "accountName":"Demo-CFD",
            "preferred":"True",
            "accountType":"CFD"
         }
      ],
      "clientId":"xxx",
      "timezoneOffset":1,
      "hasActiveDemoAccounts":"True",
      "hasActiveLiveAccounts":"True",
      "trailingStopsEnabled":"False",
      "reroutingEnvironment":"None",
      "dealingEnabled":"True"
   }
}