Python pandas json_normalize如何

时间:2016-11-02 11:13:07

标签: python json pandas transpose

我正在尝试创建一个API来从poloniex获取历史数据,以便我的神经网络学习学校项目。

我得到了一个json文件,但问题是poloinex_df是"空",所有信息都在索引/列名行中(pic realated,variable explorer),我试图以某种方式转置它,我在这里被告知,熊猫有这个功能。我阅读了文档,并在这里查看了anserws,但没有找到我想要的东西。我想制作一个数据帧,其中包括日期开放等索引和相应的数据。我怎么做?现在我收到了错误:

data_frame = json_normalize(poloinex_df, 'date' ,['high', 'low', 'open', 'close','volume','quoteVolume','weightedAverage'])
  

追踪(最近一次呼叫最后一次):

     

文件"",第1行,in       data_frame = json_normalize(poloinex_df,' date',[' high',' low',' open',' close&#39 ;,'体积'' quoteVolume'' weightedAverage'])

     

文件" C:\ ToolBox \ Anaconda2 \ lib \ site-packages \ pandas \ io \ json.py",   第761行,在json_normalize中       _recursive_extract(data,record_path,{},level = 0)

     

文件" C:\ ToolBox \ Anaconda2 \ lib \ site-packages \ pandas \ io \ json.py",   第747行,在_recursive_extract中       recs = _pull_field(obj,path [0])

     

文件" C:\ ToolBox \ Anaconda2 \ lib \ site-packages \ pandas \ io \ json.py",   第698行,在_pull_field中       结果=结果[spec]

     

TypeError:字符串索引必须是整数,而不是str

来自下载数据的变量资源管理器中的图片:

enter image description here

我必须遵循以下代码:

import chainer as chain
import pandas as pd
import numpy as np
from matplotlib import pylab
from pandas.io.json import json_normalize

def get_poloinex_data(s, a, b, c):

    import requests
    from pandas import DataFrame
    from io import StringIO

    url = 'https://poloniex.com/public?command=returnChartData'

    url += '&currencyPair=' + s #USDT for USD
    url += '&start=' + a
    url += '&end=' + b
    url += '&period=' + c

    csv = requests.get(url)

    if csv.ok:
        return DataFrame.from_csv(StringIO(csv.text), sep=',') #Separator here
    else:
        return None

params = {
    # specify stock
    "s": 'USDT_ETH',   # BTC_ETH, USDT_ETH stb

    # query data from
    'a': '1422230400',     # unix date stamp, 2015 jan 26 : 1422230400

    # query until
    'b': '1492905600',     # unix dates tamp, 2016 sep 23 : 1492905600

    # frequency
    'c': '14400'      # unix time stamp
}


poloinex_df = get_poloinex_data(**params)
data_frame = json_normalize(poloinex_df, 'date' ,['high', 'low', 'open', 'close','volume','quoteVolume','weightedAverage'])
#data_frame = json_normalize(poloinex_df.json())
#TransposedData = poloinex_df.transpose()
#data_frame = json_normalize(resp.json())

感谢您的帮助和见解

1 个答案:

答案 0 :(得分:2)

import pandas as pd
import requests

def get_poloinex_data(s, a, b, c):

    url = 'https://poloniex.com/public?command=returnChartData'

    url += '&currencyPair=' + s #USDT for USD
    url += '&start=' + a
    url += '&end=' + b
    url += '&period=' + c
    data = requests.get(url)

    return data.content

params = {
    # specify stock
    "s": 'USDT_ETH',   # BTC_ETH, USDT_ETH stb

    # query data from
    'a': '1422230400',     # unix date stamp, 2015 jan 26 : 1422230400

    # query until
    'b': '1492905600',     # unix dates tamp, 2016 sep 23 : 1492905600

    # frequency
    'c': '14400'      # unix time stamp
}

poloinex_df = get_poloinex_data(**params)
data_frame = pd.read_json(poloinex_df)