【问题标题】:Convert data frame to nested json in python在python中将数据框转换为嵌套的json
【发布时间】:2020-03-03 13:02:11
【问题描述】:

我正在尝试使用以下代码将 df 转换为嵌套 json:

nested_json = (df.groupby(['prediction_probability','id','ts','prediction_value'], as_index=False)
             .apply(lambda x:x[[
                "first_create_date",  
                "create_date",
                "update_timestamp",
                 "revenue",
                 "col",
                 "x"]].to_dict('r'))
             .reset_index()
             .rename(columns={0:'features'})
             .to_json(orient='records'))

我的问题是嵌套的 dict (key ='features') 用方括号括起来。 如何避免方括号?我知道我可以将输出视为字符串并替换方括号,但当然,这是一种不好的做法

输出:

[
    {
        "pred": 0.50726,
        "id": "0030X00002qMwFrQAKxxxx",
        "ts": "2020-02-19T20:32:15.016586",
        "value": "A",
        "features": [
            {
                "first_create_date": 1582089665000,
                "create_date": 1582089665000,
                "update_timestamp": 1582142462000,
                "revenue": null,
                "col":"aaaa",
                "x": null
            }
        ]
    },
    {
        "pred": 0.50895,
        "id": "0030X00002qMvfHQASxxxxx",
        "ts": "2020-02-19T20:32:15.016586",
        "value": "A",
        "features": [
            {
                "first_create_date": 1582077985000,
                "create_date": 1582077985000,
                "update_timestamp": 1582142462000,
                "revenue": null,
                "col":"aaaa",
                "x": null
            }
        ]
    }
]

期望的输出:

[
    {
        "pred": 0.50726,
        "id": "0030X00002qMwFrQAKxxxx",
        "ts": "2020-02-19T20:32:15.016586",
        "value": "A",
        "features": 
            {
                "first_create_date": 1582089665000,
                "create_date": 1582089665000,
                "update_timestamp": 1582142462000,
                "revenue": null,
                "col":"aaaa",
                "x": null
            }

    },
    {
        "pred": 0.50895,
        "id": "0030X00002qMvfHQASxxxxx",
        "ts": "2020-02-19T20:32:15.016586",
        "value": "A",
        "features": 
            {
                "first_create_date": 1582077985000,
                "create_date": 1582077985000,
                "update_timestamp": 1582142462000,
                "revenue": null,
                "col":"aaaa",
                "x": null
            }

    }
]

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    简单的 dict 理解就可以解决问题: 假设您可以访问一个形状类似于您的输出的嵌套 json,并将其命名为 output。然后,要达到您想要的输出,您唯一需要做的就是获取features 列表的第一个元素:

    desired_output = [{k: v if k!='features' else v[0]} for x in output for k,v in x.items()]
    

    【讨论】:

    • 谢谢你,但现在我收到了这个错误:“'str' object has no attribute 'items'”,因为 JSON 实际上是一个字符串类型
    • 我添加了这一行:format_json = json.loads(nested_json) 然后稍微改变了你的代码:desired_output = [{k: v if k != 'features' else v[0] for k, v in x.items()} for x in format_json] 并且有效!!谢谢你!
    • 不客气。如果答案解决了您的问题,请接受它(带有小“v”号),让其他人知道它已关闭。
    猜你喜欢
    • 1970-01-01
    • 2019-07-08
    • 2019-05-09
    • 1970-01-01
    • 2017-03-21
    • 2020-12-16
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多