簡體   English   中英

將嵌套的json轉換為pandas數據框

[英]Convert nested json to pandas data frame

我試圖將嵌套的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來處理缺失的值,但我的數據幀是由元組組成的。

謝謝

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM