2 回答

TA贡献1942条经验 获得超3个赞
我们开始了,这有效。可能不是最漂亮的,但它有效,我可能会在稍后回来清理它。
original = [{"patient_0_order": 1234, "patient_0_id": 123, "patient_1_id": 12, "patient_1_order": 1255}]
original = original[0]
elems = []
current_patient = 0
current_d = {}
total_elems = len(original.keys())
for index, i in enumerate(sorted(original.keys(), key=lambda x: int(x.split("_")[1]))):
key_details = i.split("_")
# This will be used in the dataframe as a column name
key_name = key_details[2]
# The number specific to this patient
patient_num = int(key_details[1])
# Checking if we're still on the same patient
if patient_num == current_patient:
current_d[key_name] = original[i]
# Checks if this is the last element
if index == total_elems-1:
elems.append(current_d)
# Checks if we've moved on to the next patient and moves on accordingly
if patient_num != current_patient:
elems.append(current_d)
# Starting off the new dictionary for this patient with the current key
current_d = {key_name: original[i]}
current_patient = patient_num
df = pd.DataFrame(elems)
并随意修改key_name方法以调整您希望列的命名方式!添加一个'patient_'到它会得到你在问题中的内容。
添加回答
举报