json的分组问题
我已经开始使用json数据。我有一个csv,所以我想按数据分组并将其转换为json,我面临一个问题json的分组问题,json,group-by,Json,Group By,我已经开始使用json数据。我有一个csv,所以我想按数据分组并将其转换为json,我面临一个问题 j = (scan4.groupby(['cluster','station','date','loadIdentifier'], as_index=True) .apply(lambda x: x[['Status','count']].to_dict('r')) .reset_index() .rename(colu
j = (scan4.groupby(['cluster','station','date','loadIdentifier'], as_index=True)
.apply(lambda x: x[['Status','count']].to_dict('r'))
.reset_index()
.rename(columns={0:'Info'})
.to_json(orient='records'))
print(json.dumps(json.loads(j), indent=4, sort_keys=False))
输出:
[
{
"cluster": "CS002",
"station": "station_2",
"date": "3/17/2021",
"loadIdentifier": "CS002-2-1615964847972",
"Info": [
{
"Status": "completed",
"count": 15
},
{
"Status": "error",
"count": 1
}
]
},
{
"cluster": "CS002",
"station": "station_2",
"date": "3/17/2021",
"loadIdentifier": "CS002-2-1615968266313",
"Info": [
{
"Status": "completed",
"count": 24
}
]
},
{
"cluster": "CS002",
"station": "station_2",
"date": "3/17/2021",
"loadIdentifier": "CS002-2-1615975866649",
"Info": [
{
"Status": "completed",
"count": 36
},
{
"Status": "error",
"count": 1
},
{
"Status": "stopped",
"count": 1
}
]
},
。。。诸如此类
但我希望输出的格式如下,以减少冗余
{
"cluster": "CS002",
"Station": "station_1",
"date": "30/04/2021":[
{
"loadIdentifer":"CS002-2-1615975866649":
[
{
"status": "completed",
"count": 90
},
{
"status": "error",
"count": 10
},
{
"status": "stopped",
"count": 1
},
{
"status": "ongoing",
"count": 90
}
]
},
{
"loadIdentifer":"CS002-2-1615968266313":
[
{
"status": "completed",
"count": 90
},
{
"status": "error",
"count": 10
},
{
"status": "stopped",
"count": 1
},
{
"status": "ongoing",
"count": 90
}
]
}
],
"date":"01/05/2021":[
{
"loadIdentifer":"CS002-2-1615964847972":
[
{
"status": "completed",
"count": 90
},
{
"status": "error",
"count": 10
},
{
"status": "stopped",
"count": 1
},
{
"status": "ongoing",
"count": 90
}
]
},
我尝试过用不同的方式对它进行分组,但最终某些或其他值会重复出现