0

I am trying to turn a request response into a pandas dataframe.

So far I have turned it into a string and just browsed Google for a bit. My first language was technically python but when I was learning python anything 3.X was experimental so it's been a while, plus pandas is a whole other beast.

ANYWAYS, trying to turn this into a dataframe, any ideas or links I should check out?:

THE BELOW IS A STRING, NOT A DICTIONARY. I even try

from ast import literal_eval
...
ast.literal_eval(data)

and this also does not work .. hmmm

{
  "instrument": "EUR_USD",
  "granularity": "S5",
  "candles": [
    {
      "complete": true,
      "volume": 7,
      "time": "2020-07-14T19:33:50.000000000Z",
      "mid": {
        "o": "1.13925",
        "h": "1.13926",
        "l": "1.13922",
        "c": "1.13922"
      }
    },
    {
      "complete": true,
      "volume": 1,
      "time": "2020-07-14T19:33:55.000000000Z",
      "mid": {
        "o": "1.13921",
        "h": "1.13921",
        "l": "1.13921",
        "c": "1.13921"
      }
    },
    {
      "complete": true,
      "volume": 1,
      "time": "2020-07-14T19:34:00.000000000Z",
      "mid": {
        "o": "1.13920",
        "h": "1.13920",
        "l": "1.13920",
        "c": "1.13920"
      }
    },
    {
      "complete": false,
      "volume": 10,
      "time": "2020-07-14T19:34:10.000000000Z",
      "mid": {
        "o": "1.13921",
        "h": "1.13921",
        "l": "1.13916",
        "c": "1.13920"
      }
    }
  ]
}
  • `pd.DataFrame(data['candles'])` reference: https://stackoverflow.com/a/53831756/4909087 – cs95 Jul 14 '20 at 19:40

1 Answers1

0

use the pandas module and the DataFrame() function from it.

a = {
  "instrument": "EUR_USD",
  "granularity": "S5",
  "candles": [
    {
      "complete": True,
      "volume": 7,
      "time": "2020-07-14T19:33:50.000000000Z",
      "mid": {
        "o": "1.13925",
        "h": "1.13926",
        "l": "1.13922",
        "c": "1.13922"
      }
    },
    {
      "complete": True,
      "volume": 1,
      "time": "2020-07-14T19:33:55.000000000Z",
      "mid": {
        "o": "1.13921",
        "h": "1.13921",
        "l": "1.13921",
        "c": "1.13921"
      }
    },
    {
      "complete": True,
      "volume": 1,
      "time": "2020-07-14T19:34:00.000000000Z",
      "mid": {
        "o": "1.13920",
        "h": "1.13920",
        "l": "1.13920",
        "c": "1.13920"
      }
    },
    {
      "complete": False,
      "volume": 10,
      "time": "2020-07-14T19:34:10.000000000Z",
      "mid": {
        "o": "1.13921",
        "h": "1.13921",
        "l": "1.13916",
        "c": "1.13920"
      }
    }
  ]
}

import pandas as pd
df = pd.DataFrame(a)
Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26