Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
json的分组问题_Json_Group By - Fatal编程技术网

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

我已经开始使用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(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
            }
        ]
    },
我尝试过用不同的方式对它进行分组,但最终某些或其他值会重复出现