将JSON数据转换为Python数据帧

2024-04-29 04:29:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将JSON数据转换为python数据帧。当我规范化JSON数据时,完整的数据存储在单个记录中作为series对象。 您能告诉我如何将以下JSON数据转换为Python数据帧吗

代码:

[{'Name':"SS",  
  'Order':[{'Type':'DO','Value':'10.11/7654326'},  
           {'Type':'UR','Value':'https://do.org/10.11/765436'}],
  'Order_Type':'dsggg',
  'Performance':[{'Per':{'Begin_Date':'2018-01-01','End_Date':'2018-02-02'},  
                  'Ins':[{'Me':'TT','Sales':2}]}]},
{'Name':"MM",
  'Order':[{'Type':'DO','Value':'10.11/7654326'},  
           {'Type':'UR','Value':'https://do.org/10.11/765436'}],
  'Order_Type':'dsggg',
  'Performance':[{'Per':{'Begin_Date':'2018-01-01','End_Date':'2018-02-02'},  
                  'Ins':[{'Me':'TT','Sales':2}]}]}

]


Tags: 数据namehttpsorgjsondatevaluetype
1条回答
网友
1楼 · 发布于 2024-04-29 04:29:23

如果不需要使用循环来获取每一个,那么可以使用Pandas

import pandas as pd
from pandas.io.json import json_normalize

data = [{'Name':"SS",  
  'Order':[{'Type':'DO','Value':'10.11/7654326'},  
           {'Type':'UR','Value':'https://do.org/10.11/765436'}],
  'Order_Type':'dsggg',
  'Performance':[{'Per':{'Begin_Date':'2018-01-01','End_Date':'2018-02-02'},  
                  'Ins':[{'Me':'TT','Sales':2}]}]},
{'Name':"MM",
  'Order':[{'Type':'DO','Value':'10.11/7654326'},  
           {'Type':'UR','Value':'https://do.org/10.11/765436'}],
  'Order_Type':'dsggg',
  'Performance':[{'Per':{'Begin_Date':'2018-01-01','End_Date':'2018-02-02'},  
                  'Ins':[{'Me':'TT','Sales':2}]}]}]

df = pd.DataFrame.from_dict(json_normalize(data), orient='columns')

相关问题 更多 >