简体   繁体   中英

How to extract data from complex json with Python?

I am trying to extract data from this json file in format like this:

    [{'from': '2019-11-22T12:45:00-05:00',
  'to': '2019-11-22T12:50:00-05:00',
  'value': [{'value': 0, 'label': 'fw'}, {'value': 0, 'label': 'bw'}]},
 {'from': '2019-11-22T12:50:00-05:00',
  'to': '2019-11-22T12:55:00-05:00',
  'value': [{'value': 0, 'label': 'fw'}, {'value': 1, 'label': 'bw'}]},
 {'from': '2019-11-22T12:55:00-05:00',
  'to': '2019-11-22T13:00:00-05:00',
  'value': [{'value': 0, 'label': 'fw'}, {'value': 0, 'label': 'bw'}]}]

The goal is to get the columns "from", "to", "value" and label" So I should have 6 row of data, 2 row for each instance of time, something like this: 在此处输入图片说明

I have tried using

pd.DataFrame function from pandas and i get output like this:

在此处输入图片说明

Any suggestion how do i go about this?

You could explode the value column, and then extract the actual label and value fields. But IMHO it is simpler and faster to pre-process data

df = pd.DataFrame([[d['from'], d['to'], d1['value'], d1['label']]
                    for d in data for d1 in d['value']],
                   columns=['from', 'to', 'value', 'label'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM