I'm writing an API endpoint in Django Rest Framework and want to format data for a graph. I have data like this which I get from the database:
data = [
    {
        "name": "Test 3",
        "status": "Active",
        "count": 1
    },
    {
        "name": "Test 2",
        "status": "Failed",
        "count": 1
    },
    {
        "name": "Test",
        "status": "In Progress",
        "count": 85
    },
    {
        "name": "Test",
        "status": "Failed",
        "count": 40
    },
    {
        "name": "Test",
        "status": "Active",
        "count": 1
    },
    {
        "name": "Test",
        "status": "Success",
        "count": 218
    },
    {
        "name": "Test 2",
        "status": "Active",
        "count": 1
    }
]
I want to format the final graph data from above like this in order to show it in the graph:
[
    "labels": ['Test', 'Test 2', 'Test 3'],
    "data": [
        {
            name: 'Active',
            data: [1, 1, 1]
        },
        {
            name: 'Failed',
            data: [40, 1, 0]
        },
        {
            name: 'Success',
            data: [218, 0, 0]
        },
        {
            name: 'In Progress',
            data: [85, 0, 0]
        }
    ]
]
I'm trying to format data in that way but I'm unable to do so. Are there any built-in functions that I can use to make the data format correct?
response = [
    {
        'labels': [],
        'data': [],
    }
]
for row in data:
    if row['name'] not in response[0]['labels']:
        response[0]['labels'].append(row['name'])
        innerData = []
        for status in ['Active', 'Failed', 'Success', 'In Progress']:
            if status in row['status']:
                innerData.append(row['count'])
            else:
                innerData.append(0)
        response[0]['data'].append(
            {
                'name': status,
                'data': innerData,
            }
        )