JSON STR:
{
"PurchaseId": "Pur-001",
"Orders": [{
"id": "154",
"isOnline": false,
"Store_location": {
"Order-Date": "2019-06-04T07:35:00"
},
"Store_Network": [{
"Network_Domain": "Food_Processing"
}]
}],
"Sales": [{
"id": "1856",
"SalesLoads": [
1000,
3000,
5000
],
"Network": [{
"id": "London_Store",
"history": [
0,
1,
2,
0,
0,
0,
0,
0
],
"Leads": {
"From": "Mgmt-Dept",
"time": "34hrs"
}
}]
}]
}
到目前为止我的代码:
import pandas.io.json as pd_json
data = pd_json.loads(json_str)
df=pd_json.json_normalize(data, record_path='loads')
我尝试了JSON_Normalize,但是无法将此JSON字符串加载到数据框中。是否可以使用JSON Normalize做到这一点,或者是否有其他优化的解决方案可用。
答案 0 :(得分:1)
这很长,但是可以完成工作。希望有人能提供更好的解决方案和更少的冗长答案。
a = {
"PurchaseId": "Pur-001",
"Orders": [{
"id": "154",
"isOnline": False,
"Store_location": {
"Order-Date": "2019-06-04T07:35:00"
},
"Store_Network": [{
"Network_Domain": "Food_Processing"
}]
}],
"Sales": [{
"id": "1856",
"SalesLoads": [
1000,
3000,
5000
],
"Network": [{
"id": "London_Store",
"history": [
0,
1,
2,
0,
0,
0,
0,
0
],
"Leads": {
"From": "Mgmt-Dept",
"time": "34hrs"
}
}]
}]}
b = pd.DataFrame.from_dict(a)
b = (b.assign(Orders_id = b.Orders[0]['id'],
Orders_isOnline = b.Orders[0]['isOnline'],
Orders_Store_Location_Number = pd.to_datetime(b.Orders[0]['Store_location']['Order-Date'].split('T')[0])
.strftime('%m/%d/%Y'),
Orders_Store_Network_Domain = b.Orders[0]['Store_Network'][0]['Network_Domain'],
Sales_id = b.Sales[0]['id'],
Sales_Load = [b.Sales[0]['SalesLoads']],
Sales_Network_id = b.Sales[0]['Network'][0]['id'],
Sales_Network_history = [b.Sales[0]['Network'][0]['history']],
Sales_Leads_from = b.Sales[0]['Network'][0]['Leads']['From'],
Sales_Lead_Time = b.Sales[0]['Network'][0]['Leads']['time']
)
.drop(['Orders','Sales'],axis=1)
)
b
答案 1 :(得分:0)
您可以直接将字符串导入到 DataFrame 中,因为您必须将字符串转换为Dictionay。 只需导入 JSON 并进行转换
json_str = json.dumps(json_data
json1_data = json.loads(data)
df= json_normalize(json1_data)