【发布时间】:2016-10-17 20:42:49
【问题描述】:
我正在尝试从 JSON 生成一个数据框。我拥有的 json 格式如下
{
eventId: "9668383e-ec96-4d6a-b873-2312dd008e7b",
eventType: "PlannedCustomerChoiceWasUpdated",
publishedDate: "2016-05-31T18:52:29.219Z",
payload: {
plannedCustomerChoiceId: "e9301a6e-7ccf-4c89-bd05-19c1b9067a61"
},
_links: {
self: {
href: "http://gbp-router.gapinc.dev:8080/planning-service/_feeds/planning.planning-service.planned-customer-choice-events/entries/d2de62a6-1e0f-430a-bf3f-2df711f64beb"
},
source: {
href: "http://gbp-router.gapinc.dev:8080/planning-service/planning/buy-plan/planned-customer-choices/e9301a6e-7ccf-4c89-bd05-19c1b9067a61"
}
}
}
我需要将这些列作为单个记录。
这是我到目前为止所做的。
from pandas.io.json import json_normalize
a = { "eventId": "9668383e-ec96-4d6a-b873-2312dd008e7b", "eventType": "PlannedCustomerChoiceWasUpdated", "publishedDate": "2016-05-31T18:52:29.219Z", "payload": { "plannedCustomerChoiceId": "e9301a6e-7ccf-4c89-bd05-19c1b9067a61" }, "_links": { "self": { "href": "http://gbp-router.gapinc.dev:8080/planning-service/_feeds/planning.planning-service.planned-customer-choice-events/entries/d2de62a6-1e0f-430a-bf3f-2df711f64beb" }, "source": { "href": "http://gbp-router.gapinc.dev:8080/planning-service/planning/buy-plan/planned-customer-choices/e9301a6e-7ccf-4c89-bd05-19c1b9067a61" } } }
b = json_normalize(a)
print b
我无法达到所需的格式。谁能帮帮我。
使用 b = pd.DataFrame(a) 数据框的格式如下。
_
links \
plannedCustomerChoiceId NaN
self {u'href': u'http://gbp-router.gapinc.dev:8080/...
source {u'href': u'http://gbp-router.gapinc.dev:8080/...
eventId \
plannedCustomerChoiceId 9668383e-ec96-4d6a-b873-2312dd008e7b
self 9668383e-ec96-4d6a-b873-2312dd008e7b
source 9668383e-ec96-4d6a-b873-2312dd008e7b
eventType \
plannedCustomerChoiceId PlannedCustomerChoiceWasUpdated
self PlannedCustomerChoiceWasUpdated
source PlannedCustomerChoiceWasUpdated
payload \
plannedCustomerChoiceId e9301a6e-7ccf-4c89-bd05-19c1b9067a61
self NaN
source NaN
publishedDate
plannedCustomerChoiceId 2016-05-31T18:52:29.219Z
self 2016-05-31T18:52:29.219Z
source 2016-05-31T18:52:29.219Z
我真正想要的是
9668383e-ec96-4d6a-b873-2312dd008e7b,PlannedCustomerChoiceWasUpdated,2016-05-31T18:52:29.219Z,e9301a6e-7ccf-4c89-bd05-19c1b9067a61,http://gbp-router.gapinc.dev:8080/planning-service/planning/buy-plan/planned-customer-choices/e9301a6e-7ccf-4c89-bd05-19c1b9067a61
【问题讨论】:
-
single records是什么意思?您可以添加所需的输出吗? -
b = pd.DataFrame(a)呢? -
基本上我希望规范化 json 并创建一个数据框,以便我可以采用表格格式。
-
EventId EventType PubliseDate PlannedCustomerChoiceID源9668383E-EC96-4D6A-B873-2312DD008E7B PlannedCustomerChoiceWasupdated 2016-05-31T18:52:29.219Z E9301A6E-7CCF-4C89-BD05-19C1B9067A61 gbp-router.gapinc.dev:8080/planning-service/planning/buy-plan/… Span>
-
b = pd.DataFrame(a) 不会为我提供表格格式。已经试过了
标签: python json string pandas dataframe