【问题标题】:How to convert Pandas Dataframe columns of String type to a SET in JSON如何将字符串类型的 Pandas Dataframe 列转换为 JSON 中的 SET
【发布时间】:2020-06-01 07:17:39
【问题描述】:

我有一个 pandas 数据框,其 to_json(orient="records") 格式的每个条目都返回:

{
    "applicationType": "IMPALA",
    "user": "root",
    "id": "705c64ad",
    "category_2": "{ \"tag\": \"uncategorised\",\"tag_type\":\"uncategorised\" }",
    "category_5": "{ \"tag\": \"HR\",\"tag_type\":\"Management\" }",
    "category_8": "{ \"tag\": \"uncategorised\",\"tag_type\":\"uncategorised\" }"
}

如果使用 to_dict(orient="records") 操作 Dataframe,它将以 Dictionary 格式返回以下数据:

{
'applicationType': 'IMPALA',
'user': 'root',
'id': '705c64ad', 
'category_2': '{ "tag": "uncategorised","tag_type":"uncategorised" }',
'category_5': '{ "tag": "HR","tag_type":"Management" }', 
'category_8': '{ "tag": "uncategorised","tag_type":"uncategorised" }'
}

我想处理上述数据以生成一个 JSON,该 JSON 应该是一个 SET,在一个名为“category”的标签中为数据框的所有“category_*”列具有唯一条目,如下所示

{
    "applicationType": "IMPALA",
    "user": "root",
    "id": "705c64ad",
    "category": [{ "tag": "uncategorised","tag_type":"uncategorised" }, { "tag": "HR","tag_type":"Management" }]
}

【问题讨论】:

    标签: python python-3.x pandas list set


    【解决方案1】:

    试试这个:

    import json
    json_data = '''{
      "applicationType": "IMPALA",
      "user": "root",
      "id": "705c64ad",
      "category_2": {
        "tag": "uncategorised",
        "tag_type": "uncategorised"
      },
      "category_5": {
        "tag": "HR",
        "tag_type": "Management"
      },
      "category_8": {
        "tag": "uncategorised",
        "tag_type": "uncategorised"
      }
    }'''
    data = json.loads(json_data)
    
    res = {x: data[x] for x in data if 'category' not in x}
    res['category'] = [dict(y) for y in {tuple(data[x].items()) for x in data if 'category' in x}]
    print(res)
    

    输出:

    {'applicationType': 'IMPALA', 'user': 'root', 'id': '705c64ad', 'category': [{'tag': 'uncategorised', 'tag_type': 'uncategorised'}, {'tag': 'HR', 'tag_type': 'Management'}]}
    

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2020-04-12
      • 1970-01-01
      • 2020-11-20
      • 2020-02-16
      • 2020-10-26
      • 2020-10-22
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多