【问题标题】:Python - How to convert an array of json objects to a Dataframe?Python - 如何将 json 对象数组转换为 Dataframe?
【发布时间】:2018-04-05 06:46:28
【问题描述】:

我对 python 完全陌生。我需要一点帮助才能过滤我的 JSON。

json = { 
    "selection":[ 
         {
          "person_id":105894,
          "position_id":1,
          "label":"Work",
          "description":"A description",
          "startDate":"2017-07-16T19:20:30+01:00",
          "stopDate":"2017-07-16T20:20:30+01:00"
          },
          {
         "person_id":945123,
         "position_id":null,
         "label":"Illness",
         "description":"A description",
         "startDate":"2017-07-17T19:20:30+01:00",
         "stopDate":"2017-07-17T20:20:30+01:00"
         }
       ]
     }

具体来说,我要做的是将我的 JSON(在上面)转换为 Dataframe,以便能够在其上使用查询方法,例如:

selected_person_id = 105894
query_person_id = json[(json['person_id'] == selected_person_id)]
or
json.query('person_id <= 105894')

列必须是:

cols = ['person_id', 'position_id', 'label', 'description', 'startDate', 'stopDate']

我该怎么做?

【问题讨论】:

标签: python json pandas


【解决方案1】:

用途:

df = pd.DataFrame(json['selection'])
print (df)
     description    label  person_id  position_id                  startDate  \
0  A description     Work     105894          1.0  2017-07-16T19:20:30+01:00   
1  A description  Illness     945123          NaN  2017-07-17T19:20:30+01:00   

                    stopDate  
0  2017-07-16T20:20:30+01:00  
1  2017-07-17T20:20:30+01:00  

编辑:

import json

with open('file.json') as data_file:    
    json = json.load(data_file)

【讨论】:

  • @Jezrael,你在哪里给出 json 的路径?
【解决方案2】:

对于需要扁平化结构的更复杂示例,请使用 json_normalize:

>>> data = [{'state': 'Florida',
...          'shortname': 'FL',
...          'info': {
...               'governor': 'Rick Scott'
...          },
...          'counties': [{'name': 'Dade', 'population': 12345},
...                      {'name': 'Broward', 'population': 40000},
...                      {'name': 'Palm Beach', 'population': 60000}]},
...         {'state': 'Ohio',
...          'shortname': 'OH',
...          'info': {
...               'governor': 'John Kasich'
...          },
...          'counties': [{'name': 'Summit', 'population': 1234},
...                       {'name': 'Cuyahoga', 'population': 1337}]}]
>>> from pandas.io.json import json_normalize
>>> result = json_normalize(data, 'counties', ['state', 'shortname',
...                                           ['info', 'governor']])
>>> result
         name  population info.governor    state shortname
0        Dade       12345    Rick Scott  Florida        FL
1     Broward       40000    Rick Scott  Florida        FL
2  Palm Beach       60000    Rick Scott  Florida        FL
3      Summit        1234   John Kasich     Ohio        OH
4    Cuyahoga        1337   John Kasich     Ohio        OH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多