将Dataframe Pandas到嵌套的json中,键作为列的值

sh7euo9m  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(121)

我的目标是将此CSV数据集转换为python中的嵌套json,按经度、纬度和时间分组,但也有数字的键,将键作为每个数字的嵌套json

longitude,latitude,number,time,u10
12.1700000763,-70.2799987793,0,2023-02-01 06:00:00,-14.959579
12.1700000763,-70.2799987793,1,2023-02-01 06:00:00,-14.733505
12.1700000763,-70.2799987793,2,2023-02-01 06:00:00,-15.036199
12.1700000763,-70.2799987793,0,2023-02-01 12:00:00,-17.041065
12.1700000763,-70.2799987793,1,2023-02-01 12:00:00,-16.244131
12.1700000763,-70.2799987793,2,2023-02-01 12:00:00,-17.173492

运行

data_frame.groupby(['latitude','longitude','time'])
.apply(lambda x: x[['u10']]
.to_dict('records'))
.reset_index()
.rename(columns={0:'number'})
.to_json(orient='records')

导致

[
    {
        "latitude": -70.2799987793,
        "longitude": 12.1700000763,
        "time": 1675231200000,
        "number": [
            {
                "u10": -14.9595794678
            },
            {
                "u10": -14.733505249
            },
            {
                "u10": -15.036198616
            }
        ]
    },
    {
        "latitude": -70.2799987793,
        "longitude": 12.1700000763,
        "time": 1675252800000,
        "number": [
            {
                "u10": -17.0410652161
            },
            {
                "u10": -16.2441310883
            },
            {
                "u10": -17.1734924316
            }
        ]
    }
]

但我想实现如下这样的嵌套json与键“数字”有键的索引作为字符串

[
    {
        "latitude": -70.2799987793,
        "longitude": 12.1700000763,
        "time": 1675231200000,
        "number": {
           "0": {
                "u10": -14.9595794678
            },
            "1": {
                "u10": -14.733505249
            },
            "2": {
                "u10": -15.036198616
            }
        }
    },
    {
        "latitude": -70.2799987793,
        "longitude": 12.1700000763,
        "time": 1675252800000,
        "number": {
            "0": {
                "u10": -17.0410652161
            },
            "1": {
                "u10": -16.2441310883
            },
            "2": {
                "u10": -17.1734924316
            }
        }
    }
]

有人能帮我一下吗,我对 Dataframe 很陌生

qxgroojn

qxgroojn1#

你可以修改lambda函数来创建一个字典,使其具有所需的形状:

json_out = df.groupby(['latitude','longitude','time']) \
        .apply(lambda x: {str(i): {'u10': e} for i, e in enumerate(x['u10'])}) \
        .reset_index() \
        .rename(columns={0:'number'}) \
        .to_json(orient='records', indent=4)

输出:

[
    {
        "latitude":-70.2799987793,
        "longitude":12.1700000763,
        "time":"2023-02-01 06:00:00",
        "number":{
            "0":{
                "u10":-14.959579
            },
            "1":{
                "u10":-14.733505
            },
            "2":{
                "u10":-15.036199
            }
        }
    },
    {
        "latitude":-70.2799987793,
        "longitude":12.1700000763,
        "time":"2023-02-01 12:00:00",
        "number":{
            "0":{
                "u10":-17.041065
            },
            "1":{
                "u10":-16.244131
            },
            "2":{
                "u10":-17.173492
            }
        }
    }
]

编辑:相同的结果,更接近原始解决方案:

json_out = df.groupby(['latitude','longitude','time']) \
        .apply(lambda x: x[['u10']].reset_index(drop=True).to_dict('index')) \
        .reset_index() \
        .rename(columns={0:'number'}) \
        .to_json(orient='records', indent=4)

相关问题