PyBoss
PyBoss

Reputation: 631

Pandas convert JSON string to Dataframe - Python

i have a json string that need to be convert to a dataframe with desired column name.

my_json = {'2017-01-03': {'open': 214.86,
  'high': 220.33,
  'low': 210.96,
  'close': 216.99,
  'volume': 5923254},
'2017-12-29': {'open': 316.18,
  'high': 316.41,
  'low': 310.0,
  'close': 311.35,
  'volume': 3777155}}

use below code doesn't give the format i want

pd.DataFrame.from_dict(json_normalize(my_json), orient='columns')

enter image description here

my expected format is below

enter image description here

Not sure how to do it?

Upvotes: 5

Views: 9789

Answers (3)

oppressionslayer
oppressionslayer

Reputation: 7204

You can also do it this way to get the exact format:

pd.DataFrame(my_json).T.rename_axis(columns='Date')                                                                                                                                  

Date          open    high     low   close     volume
2017-01-03  214.86  220.33  210.96  216.99  5923254.0
2017-12-29  316.18  316.41  310.00  311.35  3777155.0

You can also read directly from the data to get the format with the missing date:

pd.DataFrame.from_dict(my_json, orient='index').rename_axis(columns='Date')                                                                                                          

Date          open    high     low   close   volume
2017-01-03  214.86  220.33  210.96  216.99  5923254
2017-12-29  316.18  316.41  310.00  311.35  3777155

Upvotes: 4

Andy L.
Andy L.

Reputation: 25239

I assume my_json is dictionary. I.e, it it not in string format

pd.DataFrame.from_dict(my_json, orient='index').rename_axis('date').reset_index()

Out[632]:
         date    open    high     low   close   volume
0  2017-01-03  214.86  220.33  210.96  216.99  5923254
1  2017-12-29  316.18  316.41  310.00  311.35  3777155

If my_json in string format, you need call ast.iteral_eval to convert to dictionary before processing

import ast

d = ast.literal_eval(my_json)
pd.DataFrame.from_dict(d, orient='index').rename_axis('date').reset_index()

Upvotes: 3

Lambda
Lambda

Reputation: 1392

You can use DataFrame directly, then transpose it and then reset_index

pd.DataFrame(my_json).T.reset_index().rename(columns={"index":"date"})

Upvotes: 2

Related Questions