【问题标题】:Convert portions of json file to csv with pandas使用 pandas 将部分 json 文件转换为 csv
【发布时间】:2021-06-27 16:31:36
【问题描述】:

我有一个 JSON 文件,我需要从中将特定数组转换为 CSV,然后还要包含数组外部的值。这是我的示例 JSON:

{
    "activities" : [
        {
          "act_id" : "123456",
          "act_employee_logged" : {
            "emp_id" : "123",
            "emp_code" : "ABC123",
            "emp_name" : "First Last Name",
          },
          "act_type" : "ActivityType",
          "act_external_attendees" : [
            {
              "contact_id" : "Guest789",
              "contact_name" : "First Last Name",
              "contact_email" : "last.first@example.com",
              "contact_phone" : "2105555555"
            },
            {
              "contact_id" : "Guest790",
              "contact_name" : "First Last Name 1",
              "contact_email" : "last.first1@example.com",
              "contact_phone" : "2105555556"
            }
          ],
          "act_internal_attendees" : [
            {
              "att_id" : "123",
              "att_code" : "ABC123",
              "att_name" : "First Last Name",
              "att_email" : "last.first@example.com"
            },
            {
              "att_id" : "124",
              "att_code" : "ABC124",
              "att_name" : "First Last Name 1",
              "att_email" : "last.first1@example.com"
            }
          ]
        }
    ]
}

我需要将 act_external_attendees 转换为数据框,然后转换为 CSV。我已经成功地做到了,这是读取文件并创建我的数据框的代码部分:

import json
import csv
import pandas as pd

    with open("/filepath/interaction_response.json") as f:      #####  
            d = json.load(f)
    
    ext_att = pd.json_normalize(data=d['activities'], record_path='act_external_attendees', meta=['contact_id', 'contact_name', 'contact_email', 'contact_phone'],errors='ignore',record_prefix = '_') 

让我头疼的部分是我需要将act_id 值作为一列包含在我的数据框和 CSV 的每一行中,但到目前为止我还没有找到实现这一点的方法。任何建议表示赞赏。

【问题讨论】:

    标签: json pandas csv normalize


    【解决方案1】:

    这可能会有所帮助

    加载库

    import json
    import csv
    import pandas as pd
    

    读取文件

    with open("/filepath/interaction_response.json") as f:
        d = json.load(f)
    

    创建辅助结构

    my_list = []
    for sublist in myj['activities']:
        act_id = sublist['act_id']
        for val in sublist['act_external_attendees']:
            val.update({'act_id': act_id})
            my_list.append(val)
    

    结果是

    print(my_list)
    [{'contact_id': 'Guest789', 'contact_name': 'First Last Name', 'contact_email': 'last.first@example.com', 'contact_phone': '2105555555', 'act_id': '123456'}, {'contact_id': 'Guest790', 'contact_name': 'First Last Name 1', 'contact_email': 'last.first1@example.com', 'contact_phone': '2105555556', 'act_id': '123456'}]
    

    创建数据框

    df = pd.DataFrame(my_list)
    
    print(df)
      contact_id       contact_name            contact_email contact_phone  act_id
    0   Guest789    First Last Name   last.first@example.com    2105555555  123456
    1   Guest790  First Last Name 1  last.first1@example.com    2105555556  123456
    

    【讨论】:

    • 谢谢保罗。但是您的方法返回的结果与我目前得到的结果相同。我能够获取contact_id、contact_name 等,但我的问题是我还需要在每一行中包含act_id。 Act_id 在 act_external_attendees 数组之外。
    • 根据您的评论更新。
    • 谢谢保罗。这正是我想要的。我还是 Python 新手,所以我会研究解决方案并从中学习。再次感谢!
    猜你喜欢
    • 2021-12-28
    • 2018-11-06
    • 2022-01-26
    • 1970-01-01
    • 2017-09-12
    • 2017-12-17
    • 2019-03-15
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多