I have a Dataframe consisting of medical records which looks like this
My plan is to convert it to a list of dictionary(s) which would look something like this
{"parameters" : [{
"parameterCode" : 27162,
"parameterName" : "MPV (Mean Platelet Volume)",
"unit" : "fL",
"result" : 12.3,
"uRange" : 12.0,
"lRange" : 6.5
},
{
"parameterCode" : 29789,
"parameterName" : "Platelet Count",
"unit" : "thou/mm3",
"result" : 156,
"uRange" : 150.00,
"lRange" : 450.0
}]
}
Initially, I tried df.to_dict('dict')
but this makes each column label a key with the value being a dict with each key is the row index and value as the corresponding value of that column label.
(The text below would make it clear)
{
'Parameter Name': {
27162: 'MPV (Mean Platelet Volume)',
29789: 'Platelet Count',
29790: 'Segmented Neutrophils',
29791: 'Lymphocytes',
29792: 'Monocytes',
29793: 'Eosinophils',
29794: 'Basophils',
29795: 'Neutrophils',
29796: 'Lymphocytes',
29797: 'Monocytes',
29798: 'Eosinophils',
29799: 'Basophils',
29806: 'Hemoglobin',
29807: 'Packed Cell Volume (PCV)',
29808: 'Total Leukocyte Count (TLC)',
29809: 'RBC Count',
29810: 'MCV',
29811: 'MCH',
29812: 'MCHC',
29813: 'Red Cell Distribution Width (RDW)'
},
'Units': {
27162: 'fL',
29789: 'thou/mm3',
29790: '%',
29791: '%',
29792: '%',
29793: '%',
29794: '%',
29795: 'thou/mm3',
29796: 'thou/mm3',
29797: 'thou/mm3',
29798: 'thou/mm3',
29799: 'thou/mm3',
29806: 'g/dL',
29807: '%',
29808: 'thou/mm3',
29809: 'mill/mm3',
29810: 'fL',
29811: 'pg',
29812: 'g/dL',
29813: '%'
},
'Result': {
27162: '12.3',
29789: '156',
29790: '72',
29791: '22',
29792: '3.7',
29793: '1.8',
29794: '0.5',
29795: '6.74',
29796: '2.06',
29797: '0.35',
29798: '0.17',
29799: '0.05',
29806: '7',
29807: '26.7',
29808: '9.36',
29809: '3.54',
29810: '75.4',
29811: '19.8',
29812: '26.2',
29813: '17.8'
},
'Low Range': {
27162: 6.5,
29789: 150.0,
29790: 40.0,
29791: 20.0,
29792: 2.0,
29793: 1.0,
29794: nan,
29795: 2.0,
29796: 1.0,
29797: 0.2,
29798: 0.02,
29799: 0.01,
29806: 11.5,
29807: 36.0,
29808: 4.0,
29809: 3.8,
29810: 80.0,
29811: 27.0,
29812: 32.0,
29813: 11.5
},
'High Range': {
27162: 12.0,
29789: 450.0,
29790: 80.0,
29791: 40.0,
29792: 10.0,
29793: 6.0,
29794: 2.0,
29795: 7.0,
29796: 3.0,
29797: 1.0,
29798: 0.5,
29799: 0.1,
29806: 15.0,
29807: 46.0,
29808: 10.0,
29809: 4.8,
29810: 100.0,
29811: 32.0,
29812: 35.0,
29813: 14.5
}
}
My end goal is to convert this dict
to a JSON
to make it easier for our API to read it. So, if you have a better suggestion, please let me know.
I am trying to convert this medical data
to a JSON which would look something like this.
{
"labNumber": 279687825,
"age": 57,
"dataPanel": [
{
"panel_code": "Z021",
"panel_name": "COMPLETE BLOOD COUNTCBC",
"parameters": [
{
"parameterCode": 27162,
"parameterName": "MPV (Mean Platelet Volume)",
"unit": "fL",
"result": 12.3,
"uRange": 12.0,
"lRange": 6.5
},
{
"parameterCode": 29789,
"parameterName": "Platelet Count",
"unit": "thou/mm3",
"result": 156,
"uRange": 150.0,
"lRange": 450.0
}
]
}
]
}
First you need to change columns to desired output, then set orient
argument to records
when exporting to dict with to_dict().
df.columns = ["parameterCode", "parameterName", "unit", "result", "uRange", "lRange"]
res = df.to_dict(orient='records')
print(res)
[{'parameterCode': 27162, 'parameterName': 'MPV (Mean Platelet Volume)', 'unit': 'fL', 'result': '12.3', 'uRange': 6.5, 'lRange': 12.0}, {'parameterCode': 29789, 'parameterName': 'Platelet Count', 'unit': 'thou/mm3', 'result': '156', 'uRange': 150.0, 'lRange': 450.0}, {'parameterCode': 29790, 'parameterName': 'Segmented Neutrophils', 'unit': '%', 'result': '72', 'uRange': 40.0, 'lRange': 80.0}, {'parameterCode': 29791, 'parameterName': 'Lymphocytes', 'unit': '%', 'result': '22', 'uRange': 20.0, 'lRange': 40.0}, {'parameterCode': 29792, 'parameterName': 'Monocytes', 'unit': '%', 'result': '3.7', 'uRange': 2.0, 'lRange': 10.0}, {'parameterCode': 29793, 'parameterName': 'Eosinophils', 'unit': '%', 'result': '1.8', 'uRange': 1.0, 'lRange': 6.0}, {'parameterCode': 29794, 'parameterName': 'Basophils', 'unit': '%', 'result': '0.5', 'uRange': nan, 'lRange': 2.0}, {'parameterCode': 29795, 'parameterName': 'Neutrophils', 'unit': 'thou/mm3', 'result': '6.74', 'uRange': 2.0, 'lRange': 7.0}, {'parameterCode': 29796, 'parameterName': 'Lymphocytes', 'unit': 'thou/mm3', 'result': '2.06', 'uRange': 1.0, 'lRange': 3.0}, {'parameterCode': 29797, 'parameterName': 'Monocytes', 'unit': 'thou/mm3', 'result': '0.35', 'uRange': 0.2, 'lRange': 1.0}, {'parameterCode': 29798, 'parameterName': 'Eosinophils', 'unit': 'thou/mm3', 'result': '0.17', 'uRange': 0.02, 'lRange': 0.5}, {'parameterCode': 29799, 'parameterName': 'Basophils', 'unit': 'thou/mm3', 'result': '0.05', 'uRange': 0.01, 'lRange': 0.1}, {'parameterCode': 29806, 'parameterName': 'Hemoglobin', 'unit': 'g/dL', 'result': '7', 'uRange': 11.5, 'lRange': 15.0}, {'parameterCode': 29807, 'parameterName': 'Packed Cell Volume (PCV)', 'unit': '%', 'result': '26.7', 'uRange': 36.0, 'lRange': 46.0}, {'parameterCode': 29808, 'parameterName': 'Total Leukocyte Count (TLC)', 'unit': 'thou/mm3', 'result': '9.36', 'uRange': 4.0, 'lRange': 10.0}, {'parameterCode': 29809, 'parameterName': 'RBC Count', 'unit': 'mill/mm3', 'result': '3.54', 'uRange': 3.8, 'lRange': 4.8}, {'parameterCode': 29810, 'parameterName': 'MCV', 'unit': 'fL', 'result': '75.4', 'uRange': 80.0, 'lRange': 100.0}, {'parameterCode': 29811, 'parameterName': 'MCH', 'unit': 'pg', 'result': '19.8', 'uRange': 27.0, 'lRange': 32.0}, {'parameterCode': 29812, 'parameterName': 'MCHC', 'unit': 'g/dL', 'result': '26.2', 'uRange': 32.0, 'lRange': 35.0}, {'parameterCode': 29813, 'parameterName': 'Red Cell Distribution Width (RDW)', 'unit': '%', 'result': '17.8', 'uRange': 11.5, 'lRange': 14.5}]