将 pandas 数据框转换为字典列表

Converting a pandas Dataframe to a list of dictionary

我有一个由医疗记录组成的 Dataframe,如下所示

我的计划是将其转换为字典列表,看起来像这样

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

最初,我尝试了 df.to_dict('dict') 但这使得每个列标签成为一个键,其值是一个字典,每个键是行索引,值是该列标签的对应值。

(下面的文字会说清楚)

{
'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
}
}

关于我正在尝试的一些背景知识(不是问题的一部分)

我的最终目标是将此 dict 转换为 JSON 以便我们的 API 更容易阅读。所以,如果你有更好的建议,请告诉我。

我正在尝试转换此医疗数据

到 JSON 看起来像这样。

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

查看 to_dict(方向 = 'records')

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_dict.html

首先您需要将列更改为所需的输出,然后在使用 to_dict().

导出到字典时将 orient 参数设置为 records
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}]