我的目标是将此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 很陌生
1条答案
按热度按时间qxgroojn1#
你可以修改lambda函数来创建一个字典,使其具有所需的形状:
输出:
编辑:相同的结果,更接近原始解决方案: