4

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,
            }
        )
aaron
  • 39,695
  • 6
  • 46
  • 102
Hassan Shahbaz
  • 596
  • 1
  • 14
  • 38

2 Answers2

3

Building on Rustam's answer:

  1. Add a dictionary label_to_idx to be used to determine the index by label.
  2. Pass a lambda function to defaultdict to initialize lists of correct size and default value.
  3. Populate d for each status, using label_to_idx to find the correct index to fill with count.
from collections import defaultdict

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},
]

labels = sorted({each['name'] for each in data})
label_to_idx = {label: idx for idx, label in enumerate(labels)}    # Added
d = defaultdict(lambda: [0] * len(labels))                         # Modified

for each in data:
    d[each['status']][label_to_idx[each['name']]] = each['count']  # Modified

final_result = {'labels': labels, 'data': []}
for k, v in d.items():
    final_result['data'].append({'name': k, 'data': v})
from pprint import pprint as pp

pp(final_result)
# {'data': [{'data': [1, 1, 1], 'name': 'Active'},
#           {'data': [40, 1, 0], 'name': 'Failed'},
#           {'data': [85, 0, 0], 'name': 'In Progress'},
#           {'data': [218, 0, 0], 'name': 'Success'}],
#  'labels': ['Test', 'Test 2', 'Test 3']}
aaron
  • 39,695
  • 6
  • 46
  • 102
  • thanks for the answer can you please explain one thing what kind of data structure will this line make, multidimensional array or what? d = defaultdict(lambda: [0] * len(labels)) – Hassan Shahbaz Oct 20 '21 at 11:49
  • It makes a dictionary that, if key is not found, sets and returns a list of as many 0s as the number of labels. – aaron Oct 20 '21 at 12:24
2

In order to get unique labels, set comprehension can be used. Then you can use defaultdict to from key:value pair as each status being key name and count being corresponding values.

from collections import defaultdict

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}]

d = defaultdict(list)
labels = sorted({each['name'] for each in data})

for each in data:
    d[each['status']].append(each['count'])
# -> defaultdict(<class 'list'>, {'Active': [1, 1, 1], 'Failed': [1, 40], 'In Progress': [85], 'Success': [218]})

final_result = {'labels': labels, 'data': []}
for k, v in d.items():
    final_result['data'].append({'name': k, 'data': v})

final_result would look like:

{'labels': ['Test', 'Test 2', 'Test 3'], 'data': [{'name': 'Active', 'data': [1, 1, 1]}, {'name': 'Failed', 'data': [1, 40]}, {'name': 'In Progress', 'data': [85]}, {'name': 'Success', 'data': [218]}]}

In order to fill small sized lists with zeros, refer to this answer.

Rustam Garayev
  • 2,632
  • 1
  • 9
  • 13
  • thanks for the answer but filling zeros at the end will not give correct data i mean correct position of zeros how can i append 0 in the list at the start so that data remain correct? – Hassan Shahbaz Oct 18 '21 at 16:17