我有一个Python pandas的数据框架,其中包含NoSQL文档和json字符串列,我想将其转换为具有特定键作为列名和值作为行中的数据的平面表。下面是我开始使用的示例表:
<表类>
诊断
tbody><<tr>1 2b 表类>
尝试将其放入lambda函数中。循环遍历字典以分离值。假设主要诊断总是单一值,但可以有1个或多个次要诊断。
data_dict = [{"icd10":"I611","icd10Name":"Intracerebral haemorrhage in hemisphere, cortical","diagType":"1","diagTypeName":"Principal Diagnosis"},{"icd10":"I10","icd10Name":"Essential (primary) hypertension","diagType":"2","diagTypeName":"Comorbidity (โรคอื่นที่เป็นร่วมด้วย)"},{"icd10":"E789","icd10Name":"Disorder of lipoprotein metabolism,unspecified","diagType":"2","diagTypeName":"Comorbidity (โรคอื่นที่เป็นร่วมด้วย)"}]
co_diag = []
for d in data_dict:
if 'Principal Diagnosis' in d.values():
pd = d['icd10']
else:
co_diag.append(d['icd10'])
co_diag_str = ', '.join(co_diag)
中的步骤,你应该能够写你需要的列。
循环时输出:
In [19]: co_diag_str
Out[19]: 'I10, E789'
In [20]: pd
Out[20]: 'I611'