【问题标题】:how to convert this nested JSON in columnar form into Pandas dataframe如何将此嵌套的 JSON 以柱状形式转换为 Pandas 数据框
【发布时间】:2015-04-03 21:02:29
【问题描述】:

我可以将这种列式格式的嵌套 JSON 格式读入 pandas。

JSON Scheme

JSON 方案格式

Python 脚本

    req = requests.get(REQUEST_API)
    returned_data = json.loads(req.text)
    # status
    print("status: {0}".format(returned_data["status"]))
    # api version
    print("version: {0}".format(returned_data["version"]))
    data_in_columnar_form = pd.DataFrame(returned_data["data"])
    data = data_in_columnar_form["data"]

更新

我想把下面的JSON方案转换成表格格式作为表格,怎么做?

JSON 方案

     "data":[  
        {  
           "year":"2009",
           "values":[  
              {  
                 "Actual":"(0.2)"
              },
              {  
                 "Upper End of Range":"-"
              },
              {  
                 "Upper End of Central Tendency":"-"
              },
              {  
                 "Lower End of Central Tendency":"-"
              },
              {  
                 "Lower End of Range":"-"
              }
           ]
        },
        {  
           "year":"2010",
           "values":[  
              {  
                 "Actual":"2.8"
              },
              {  
                 "Upper End of Range":"-"
              },
              {  
                 "Upper End of Central Tendency":"-"
              },
              {  
                 "Lower End of Central Tendency":"-"
              },
              {  
                 "Lower End of Range":"-"
              }
           ]
        },...
        ]

【问题讨论】:

标签: python pandas


【解决方案1】:

Pandas 有一个 JSON normalization 函数(从 0.13 开始),直接来自文档:

In [205]: from pandas.io.json import json_normalize

In [206]: 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}]}]
   .....: 

In [207]: json_normalize(data, 'counties', ['state', 'shortname', ['info', 'governor']])
Out[207]: 
         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

【讨论】:

  • 我总是忘记它的存在,所以我重新实现了几次。 :-(
  • @DSM 我也是...公平地说,它是“最近的”。 :)
  • @AndyHayden 谢谢你的回复,你能给我一些新问题的提示吗stackoverflow.com/questions/28335321/…
  • @Andy Hayden 如果'counties''info' 的孩子怎么办?元数据会是什么?我仍在测试以解析 'info':['counties'] 值。
猜你喜欢
  • 2020-11-12
  • 2020-01-03
  • 2017-03-21
  • 2020-12-16
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 2021-01-02
相关资源
最近更新 更多