簡體   English   中英

將JSON嵌套到熊貓數據框

[英]Nested json to pandas dataframe

我有一個嵌套的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 pandas.DataFrame(list)的數據pandas.DataFrame(list) ,這不是我想要的。

任何幫助和建議,將不勝感激。

'這是您可以如何轉換json對象(字典)的方法:

old_dict = json.loads('YOUR JSON STRING')[0]
col_names = ['order', 'id', 'time', 'desc']
# Reorganize the dictionary.
new_dict = {col: {k: v for k, v in old_dict.iteritems() if col in k} for col in col_names}
df = pd.DataFrame(new_dict)

應該返回你想要的。

我們開始,這有效。 可能不是最漂亮的方法,但是它可以正常工作,稍后我可能會再清理一次。

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_'將得到您在問題中所擁有的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM