猿问

将 json 嵌套到 Pandas 数据框

我有一个嵌套的 JSON 文件,我将它展平并得到一个看起来像这样的列表;


[{patient_0_order: 1234,

   patient_0_id: a1,

   patient_0_time: 01/01/2016,

   patient_0_desc: xyz,

   patient_1_order: 2313,

   patient_1_id: b1,

   patient_1_time: 02/01/2016,

   patient_1_desc: def,

   patient_2_order: 9876,

   patient_2_id: c1,

   patient_2_time: 03/01/2016,

   patient_2_desc: ghi,

   patient_3_order: 0075,

   patient_3_id: d1,

   patient_3_time: 04/01/2016,

   patient_3_desc: klm,

   patient_4_order: 6268,

   patient_4_id: e1,

   patient_4_time: 05/01/2016,

   patient_4_desc: pqr}`]

现在我想将列表转换为一个数据框,这样每一行都需要一个病人,如下所示。


       patient_order    patient_id       patient_time    patient_desc 

  0      1234                a1          01/01/2016        xyz

  1      2313                b1          02/01/2016        def

  2      9876                c1          03/01/2016        ghi

  3      0075                d1          04/01/2016        klm

  4      6268                e1          05/01/2016        pqr 

我尝试使用pandas.DataFrame(list),它给了我一个带有 1 行 * 20 列表的数据框,这不是我想要的。


任何帮助和建议将不胜感激。


茅侃侃
浏览 146回答 2
2回答

手掌心

我们开始了,这有效。可能不是最漂亮的,但它有效,我可能会在稍后回来清理它。original = [{"patient_0_order": 1234, "patient_0_id": 123, "patient_1_id": 12, "patient_1_order": 1255}]original = original[0]elems = []current_patient = 0current_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_numdf = pd.DataFrame(elems)并随意修改key_name方法以调整您希望列的命名方式!添加一个'patient_'到它会得到你在问题中的内容。
随时随地看视频慕课网APP

相关分类

Python
我要回答