Я пытаюсь создать JSON из приведенной ниже структуры.
Пример данных:
Country|SegmentID|total_cnt|max_value|
+---------+---------+---------+---------+
| Pune| 1| 10.0| 15|
| Delhi| 1| 10.0| 15|
|Bangalore| 1| 10.0| 15|
| Pune| 2| 10.0| 16|
| Delhi| 2| 10.0| 16|
|Bangalore| 2| 10.0| 16|
| Pune| 3| 15.0| 16|
| Delhi| 3| 10.0| 16|
|Bangalore| 3| 15.0| 16|
+---------+---------+---------+---------+
Вот мой код:
![enter image description here](https://i.stack.imgur.com/nIFc8.png)
Ожидаемая структура JSON:
[{
"NAME": "SEG1",
"VAL": 15,
"CITIES": {
"Bangalore": 10,
"Delhi": 10,
"Pune": 10
}
},
{
"NAME": "SEG2",
"VAL": 16,
"CITIES": {
"Bangalore": 10,
"Delhi": 10,
"Pune": 10
}
},
{
"NAME": "SEG3",
"VAL": 16,
"CITIES": {
"Bangalore": 15,
"Delhi": 10,
"Pune": 15
}
}
]
Я могу создать одноуровневую иерархию, но это также не удовлетворяет моим требованиям.
join_df=join_df.toPandas()
j = (join_df.groupby(['SegmentID','max_value'], as_index=False)
.apply(lambda x: x[['Country','total_cnt']].to_dict('r'))
.reset_index().rename(columns={0:'CITIES'})
.to_json(orient='records'))
Это дает такой результат:
[{"SegmentID":1,"max_value":15,"Cities":[{"Country":"Pune","total_cnt":10.0},{"Country":"Delhi","total_cnt":10.0},{"Country":"Bangalore","total_cnt":10.0}]},{"SegmentID":2,"max_value":16,"Cities":[{"Country":"Pune","total_cnt":10.0},{"Country":"Delhi","total_cnt":10.0},{"Country":"Bangalore","total_cnt":10.0}]},{"SegmentID":3,"max_value":16,"Cities":[{"Country":"Pune","total_cnt":15.0},{"Country":"Delhi","total_cnt":10.0},{"Country":"Bangalore","total_cnt":15.0}]}]