2016-11-02 8 views
1

Ich versuche, eine API zu erstellen, um historische Daten von Poloniex für meine neuralnetwork zu erhalten für ein Schulprojekt zu lernen.Python panda json_normalize wie zu

Ich bekomme eine JSON-Datei zurück, aber das Problem ist, dass die poloinex_df ist "leer", alle Informationen sind in der Reihe index/columnames (pic realated, Variable Explorer), ich habe versucht, es irgendwie zu transponieren, dann habe ich wurde hier gesagt, dass Panda eine Funktion dafür hat. Ich las die Dokumentation und schaute mich hier nach einer Antwort um, aber ich fand nicht ganz, wonach ich suchte. Ich würde gerne einen Datenrahmen mit Indizes wie Daten öffnen, schließen volumne usw. und die entsprechenden Daten. Wie mache ich das? Im Moment habe ich den Fehler:

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

Traceback (most recent call last):

File "", line 1, in data_frame = json_normalize(poloinex_df, 'date' ,['high', 'low', 'open', 'close','volume','quoteVolume','weightedAverage'])

File "C:\ToolBox\Anaconda2\lib\site-packages\pandas\io\json.py", line 761, in json_normalize _recursive_extract(data, record_path, {}, level=0)

File "C:\ToolBox\Anaconda2\lib\site-packages\pandas\io\json.py", line 747, in _recursive_extract recs = _pull_field(obj, path[0])

File "C:\ToolBox\Anaconda2\lib\site-packages\pandas\io\json.py", line 698, in _pull_field result = result[spec]

TypeError: string indices must be integers, not str

Pic in Variable Explorer von heruntergeladenen Daten:

enter image description here

ich folgenden Code haben:

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()) 

Vielen Dank für Ihre Hilfe und Einsicht

Antwort

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) 
+0

Vielen Dank, ich habe read_json irgendwie vermisst ... –

Verwandte Themen