代码之家  ›  专栏  ›  技术社区  ›  kms

dict到pandas数据帧-由?

  •  0
  • kms  · 技术社区  · 2 年前

    我有一本字典,正试着读/存为 pandas 数据帧。

    import pandas as pd
    
    j = {
         'fields': [
                    {'type': 'text', 'id': 'Status'},
                    {'type': 'text', 'id': 'gx_location'},
                    {'type': 'text', 'id': 'ASSESSORS_PARCEL_NUMBER'},
                    {'type': 'text', 'id': 'APPLICANT'}
                   ],
         'records': [['Active',
                      '     ,   ',
                      '0',
                      '1759764'],
                     ['Active',
                      '     ,   ',
                      '0',
                      '1759264'],
                     ['Active',
                      '0  CHERRY AV  , SAN JOSE CA 95125-0000',
                      '0',
                      '1759264']]
       }
    
    pdf = pd.DataFrame.from_dict(j, orient="index")
    

    当我运行上面的行时,我得到了2行带索引的行 fields records .现成的 orient 选项不起作用;因为我想得到 id 从…起 领域 作为列标签和数据 记录 钥匙

    Status      gx_location ASSESSORS_PARCEL_NUMBER APPLICANT
    Active             ,                          0   1759764
    Active             ,                          0   1759264
    Active  0  CHERRY AV...                       0   1759264
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   wjandrea senderle    2 年前

    熊猫方便快捷方式很好,但当你的需求不符合它们非常狭窄的参数时,把它们扔掉,只需完成任务。幸运的是,您所需要的非常简单:

    df = pd.DataFrame(j['records'], columns=[k['id'] for k in j['fields']])
    print(df)
    

    输出

       Status                             gx_location ASSESSORS_PARCEL_NUMBER APPLICANT
    0  Active                                    ,                          0   1759764
    1  Active                                    ,                          0   1759264
    2  Active  0  CHERRY AV  , SAN JOSE CA 95125-0000                       0   1759264