0

How can be the following dict (data came from an API call via JSON format):

{
   "AA":[
      {
         "buy":10,
         "hold":6,
         "period":"2020-12-01",
         "sell":0,
         "strongBuy":6,
         "strongSell":0,
         "symbol":"AA"
      },
      {
         "buy":8,
         "hold":8,
         "period":"2020-11-01",
         "sell":0,
         "strongBuy":4,
         "strongSell":0,
         "symbol":"AA"
      },
.......
"AAPL":[
      {
         "buy":10,
         "hold":6,
         "period":"2020-12-01",
         "sell":0,
         "strongBuy":6,
         "strongSell":0,
         "symbol":"AA"
      },

be converted to a table/dataframe structure:

| buy | .... | period     | symbol
| 10  | .... | 01-12-2020 | AA
| 8   | .... | 01-11-2020 | AA
.......
| 10  | .... | 01-12-2020 | AAPL

Just using df = pd.DataFrame.from_dict(j,orient='index') won't work here.

  • You can use pd.json_normalize(request_data) more info here: [https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe](https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe) – LaTreb Dec 11 '20 at 10:31
  • @clauhamburg, this will get you what you want `pd.DataFrame.from_dict(j,orient='index').stack().apply(pd.Series)` – user6386471 Dec 11 '20 at 10:47

0 Answers0