【问题标题】:Convert nested json to pandas data frame将嵌套的 json 转换为 pandas 数据框
【发布时间】:2016-03-08 03:51:15
【问题描述】:

我正在尝试将嵌套的 json 数组转换为 pandas 数据框。

列表格式的数据如下所示:

 [{u'analysis': {u'active': u'Y',
  u'dpv_cmra': u'N',
  u'dpv_footnotes': u'AAN1',
  u'dpv_match_code': u'D',
  u'dpv_vacant': u'N',
  u'footnotes': u'H#'},
  u'candidate_index': 0,
  u'components': 
    {u'city_name': u'City',
     u'delivery_point': u'Variable',
     u'delivery_point_check_digit': u'8',
     u'plus4_code': u'Variable',
     u'primary_number': u'Variable',
     u'state_abbreviation': u'Variable',
     u'street_name': u'Variable',
     u'street_predirection': u'Variable',
     u'street_suffix': u'Variable',
     u'zipcode': u'Variable'},
  u'delivery_line_1': u'Variable',
  u'delivery_point_barcode': u'Variable',
  u'input_id': u'Variable',
  u'input_index': Variable,
  u'last_line': u'Variable',
  u'metadata': 
    {u'building_default_indicator': u'Variable',
     u'carrier_route': u'Variable',
     u'congressional_district': u'Variable',
     u'county_fips': u'Variable',
     u'county_name': u'Variable',
     u'dst': True,
     u'zip_type': u'Variable'}}],

任何建议我如何将其转换为数据框并处理空值?我尝试使用 try / except 来处理缺失值,但我的数据框随后由元组组成。

谢谢

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    pd.io.json 中有一个json_normalize 函数。

    d = {u'analysis': {u'active': u'Y', u'dpv_cmra': u'N', u'dpv_footnotes': u'AAN1', u'dpv_match_code': u'D', u'dpv_vacant': u'N', u'footnotes': u'H#'}, u'candidate_index': 0, u'components': {u'city_name': u'City', u'delivery_point': u'Variable', u'delivery_point_check_digit': u'8', u'plus4_code': u'Variable', u'primary_number': u'Variable', u'state_abbreviation': u'Variable', u'street_name': u'Variable', u'street_predirection': u'Variable', u'street_suffix': u'Variable', u'zipcode': u'Variable'}, u'delivery_line_1': u'Variable', u'delivery_point_barcode': u'Variable', u'input_id': u'Variable', u'input_index': u'Variable', u'last_line': u'Variable', u'metadata': {u'building_default_indicator': u'Variable', u'carrier_route': u'Variable', u'congressional_district': u'Variable', u'county_fips': u'Variable', u'county_name': u'Variable', u'dst': True, u'zip_type': u'Variable'}}
    
    >>> pd.io.json.json_normalize(d)
      analysis.active analysis.dpv_cmra analysis.dpv_footnotes analysis.dpv_match_code analysis.dpv_vacant analysis.footnotes  candidate_index components.city_name components.delivery_point components.delivery_point_check_digit        ...         \
    0               Y                 N                   AAN1                       D                   N                 H#                0                 City                  Variable                                     8        ...          
    
       input_id input_index last_line metadata.building_default_indicator metadata.carrier_route metadata.congressional_district metadata.county_fips metadata.county_name metadata.dst metadata.zip_type  
    0  Variable    Variable  Variable                            Variable               Variable                        Variable             Variable             Variable         True          Variable  
    
    [1 rows x 29 columns]
    

    【讨论】:

    • 非常酷...... pandas io 再次领先于其他任何东西
    • 这似乎可行,但我的列表索引超出范围?
    • 没关系,把它理顺。感谢您的帮助!
    • 太糟糕了,它不能处理数组/列表
    猜你喜欢
    • 2017-03-21
    • 2020-12-16
    • 1970-01-01
    • 2019-11-24
    • 2022-01-21
    • 2019-05-12
    • 2020-11-12
    • 2021-12-01
    相关资源
    最近更新 更多