簡體   English   中英

如何將字符串類型的 Pandas Dataframe 列轉換為 JSON 中的 SET

[英]How to convert Pandas Dataframe columns of String type to a SET in JSON

我有一個 pandas dataframe ,其 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\" }"
}

如果 Dataframe 使用 to_dict(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" }'
}

我想處理上述數據以生成 JSON,它應該是一個 SET,在 dataframe 的所有“category_*”列的名為“category”的標簽中具有唯一條目,如下所示

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

嘗試這個:

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)

Output:

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

暫無
暫無

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

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