【问题标题】:How to group certain fields in a json under new key?如何在新键下对 json 中的某些字段进行分组?
【发布时间】:2021-06-16 15:24:47
【问题描述】:

我有一个 JSON,我想将一些字段分组到一个新键下。我怎么能在python中做到这一点? 这是初始数据和所需的输出 - 我想添加新的关键“百分比”,以便将年份分组:

{"data": [{
        "id": 1,
        "search_key": "London",
        "2021": 0.018,
        "2022": 0.019,
        "2023": 0.02,
        "2024": 0.02,
        "2025": 0.02,
        "2026": 0.02,
        "2027 and next": 0.02
    }, {
       ...
       }]}
**desired:**  
 {"data": [{
        "id": 1,
        "search_key": "London",
        "percents":{
                       "2021": 0.018,
                       "2022": 0.019,
                       "2023": 0.02,
                       "2024": 0.02,
                       "2025": 0.02,
                       "2026": 0.02,
                       "2027 and next": 0.02
                       }    
     }, {
         ...
         }]}

【问题讨论】:

  • 您的括号 [{ 不匹配。请修复您的代码。
  • 另外,请更具体。 “将某些字段分组到一个新键下”是什么意思?
  • 到目前为止你尝试了什么?
  • 我在考虑 group by 但它只适用于 agg func。

标签: python json dictionary group-by nested


【解决方案1】:

虽然它可能不漂亮,但下面的代码应该能够完成这项任务。

首先,我们遍历字典并使用正则表达式来查找格式为 year 的键。一旦我们找到这些键,我们将键值对添加到一个名为dates 的新字典中。我们还需要从字典中删除这些日期。现在我们有两个字典,datesnewData - {'data': [{'id': 1, 'search_key': 'London'}]}

现在我们将 dates 添加到 newData 并使用额外的密钥 percents

import re
data = {"data": [{
        "id": 1,
        "search_key": "London",
        "2021": 0.018,
        "2022": 0.019,
        "2023": 0.02,
        "2024": 0.02,
        "2025": 0.02,
        "2026": 0.02
        }]
}
dates = {k:v for k,v in data['data'][0].items() if re.match(r'.*([1-3][0-9]{3})', k) is not None}
newData = {"data":[{k:v for k,v in data['data'][0].items() if k not in dates}]}
newData['data'][0]['percents'] = dates

输出

{'data': [{'id': 1, 'search_key': 'London', 'percents': {'2021': 0.018, '2022': 0.019, '2023': 0.02, '2024': 0.02, '2025': 0.02, '2026': 0.02}}]}

【讨论】:

  • 谢谢!但我认为正则表达式有点过分,因为除了“2021”、“2022”、“2023”、“2024”、“2025”、“2026”、“2027 和下一个”之外,我没有其他年份了,所以这些其余 json 的键是相同的。
  • 我知道,但是如果将来的用户想要对某些键进行分组,这个答案应该会对他们有所帮助。这些键需要以某种方式是唯一的。但可以肯定。
  • 很抱歉,我实际上没有提供代码。现在可以了!
【解决方案2】:

这很难看,但确实有效。

for i in c["data"]:
    i['percent'] = {}
    if i["2021"]:
        i['percent'].update({"2021":i['2021']})
        del i["2021"]
    if i["2022"]:
        i['percent'].update({"2022":i['2022']})
        del i["2022"]
    if i["2023"]:
        i['percent'].update({"2023":i['2023']})
        del i["2023"]
    if i["2024"]:
        i['percent'].update({"2024":i['2024']})
        del i["2024"]
    if i["2025"]:
        i['percent'].update({"2025":i['2025']})
        del i["2025"]
    if i["2026"]:
        i['percent'].update({"2026":i['2026']})
        del i["2026"]
    if i["2027 and next"]:
        i['percent'].update({"2027 and next":i["2027 and next"]})
        del i["2027 and next"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多