2016-12-29 154 views
2

我與格式化一樣低於許多嵌套字典的JSON響應工作:Python的JSON到大熊貓數據幀

{u'addresses': [], 
u'application_ids': [20855193], 
u'applications': [{u'answers': [{u'answer': u'Indeed ', 
            u'question': u'How did you hear?'}], 
        u'applied_at': u'2015-10-29T22:19:04.925Z', 
        u'candidate_id': 9999999, 
        u'credited_to': None, 
        u'current_stage': {u'id': 9999999, 
             u'name': u'Application Review'}, 
        u'id': 9999999, 
        u'jobs': [{u'id': 9999999,u'name': u'ENGINEER'}], 
        u'last_activity_at': u'2015-10-29T22:19:04.767Z', 
        u'prospect': False, 
        u'rejected_at': None, 
        u'rejection_details': None, 
        u'rejection_reason': None, 
        u'source': {u'id': 7, u'public_name': u'Indeed'}, 
        u'status': u'active'}], 
u'attachments': [{u'filename': u'Jason_Bourne.pdf', 
        u'type': u'resume', 
        u'url': u'https://resumeURL'}], 
u'company': None, 
u'coordinator': {u'employee_id': None, 
        u'id': 9999999, 
        u'name': u'Batman_Robin'}, 
u'email_addresses': [{u'type': u'personal', 
         u'value': u'[email protected]'}], 
u'first_name': u'Jason', 
u'id': 9999999, 
u'last_activity': u'2015-10-29T22:19:04.767Z', 
u'last_name': u'Bourne', 
u'website_addresses': []} 

我試圖JSON的扁平化到一個表,並在發現下面的例子熊貓文檔:

http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.io.json.json_normalize.html

出於某種原因,直接在「應用程序的報頭返回如每行的一個字符下任何數據。例如,如果我稱之爲:

timeapplied = json_normalize(data,['applications', ['applied_at']]) 

我得到:

0 
0 2 
1 0 
2 1 
3 5 
4 - 
5 1 
6 0 
7 - 
8 2 
9 9 
10 T 
11 2 
12 2 
13 : 
14 1 
15 9 
16 : 
17 0 
18 4 
19 . 
20 9 
21 2 
22 5 
23 Z 

有沒有解決這個辦法,所以我可以使用normalize功能?

謝謝!

+0

你想要的輸出是什麼? – Psidom

回答

0

您的來電:

timeapplied = json_normalize(data,['applications', ['applied_at']]) 

的調用json_normalize由如下所示的參數,

pandas.io.json.json_normalize(data, record_path=None, meta=None, meta_prefix=None, record_prefix=None) 

你傳入['applications', ['applied_at']]作爲record_path。顯然,這意味着在data['applications]['applied_at']下提供的數據被用作一系列記錄。在這種情況下,字符串被用作字符列表。因此,您可以獲取與每個字符對應的行。

爲了簡單地獲得了「應用程序的標頭,如一個數據幀之下的所有數據,使用:

applied = json_normalize(data, 'applications') 

爲了獲得applied_at作爲一個單獨的柱,然後使用:

applied_at = applied.applied_at 

applied_at = applied['applied_at']