1

reference

I have a dict with output in this form:

response['candles'][0:2]

[{'complete': True,
  'mid': {'c': '0.91535', 'h': '0.91535', 'l': '0.91535', 'o': '0.91535'},
  'time': '2002-05-06T21:00:00.000000000Z',
  'volume': 1},
 {'complete': True,
  'mid': {'c': '0.90435', 'h': '0.90435', 'l': '0.90435', 'o': '0.90435'},
  'time': '2002-05-07T21:00:00.000000000Z',
  'volume': 1}]

I can create a DataFrame easily that looks like this:

res = pd.DataFrame(response['candles'])

complete    mid time    volume
0   True    {'o': '0.91535', 'h': '0.91535', 'l': '0.91535...   2002-05-06T21:00:00.000000000Z  1
1   True    {'o': '0.90435', 'h': '0.90435', 'l': '0.90435...   2002-05-07T21:00:00.000000000Z  1

I can access 'mid' dict items like this:

response['candles'][0]['mid']['c']

'0.91535'

But how do I add columns in my DataFrame for each 'mid' item, instead of having a dict inside of the DataFrame? I want a column for ['mid']['o'], ['mid']['h'], and so on.

Thanks

cadig
  • 101
  • 2
  • 8

1 Answers1

1

Use pandas.io.json.json_normalize:

d = [{'complete': True,
  'mid': {'c': '0.91535', 'h': '0.91535', 'l': '0.91535', 'o': '0.91535'},
  'time': '2002-05-06T21:00:00.000000000Z',
  'volume': 1},
 {'complete': True,
  'mid': {'c': '0.90435', 'h': '0.90435', 'l': '0.90435', 'o': '0.90435'},
  'time': '2002-05-07T21:00:00.000000000Z',
  'volume': 1}]

pd.io.json.json_normalize(d)
#complete     mid.c   mid.h   mid.l   mid.o                           time  volume
#0  True    0.91535 0.91535 0.91535 0.91535 2002-05-06T21:00:00.000000000Z  1
#1  True    0.90435 0.90435 0.90435 0.90435 2002-05-07T21:00:00.000000000Z  1
Psidom
  • 209,562
  • 33
  • 339
  • 356