python - 将嵌套的 json 转换为 pandas 数据框

标签 python pandas

我正在尝试将嵌套的 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 来处理缺失值,但我的数据框随后由元组组成。

谢谢

最佳答案

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

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]

关于python - 将嵌套的 json 转换为 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35858611/

相关文章:

python - 如何在 tensorflow 2.0 中使用预制的密集层进行训练?

python - 使用不同格式的 csv 中的数据更新 csv

python - 根据 Pandas 中一列的总和添加新列并按其他 2 列分组

python - 运行测试前的 Pytest 和数据库清理

Python3 print() 与 Python2 打印

python - Mercurial/Python - 下划线函数有什么作用?

python - 将列中的任意字符串替换为 1

python - 使用 Pandas 创建带 Series 的 DataFrame,导致内存错误

python - pandas 从第二个数据框中选择列,其中另一列的值存在于主数据框中

python - 如何查看设备文件挂载到哪里?