2016-11-07 112 views
0
import json 
import pandas as pd 

data = """ 
{ 
    "name": "Wes", 
    "place_lived": ["United Stats", "Spain", "Germany"], 
    "pet": null, 
    "Siblings": [ 
     {"name": "Scott", "age": 25, "pet": "Zuko"}, 
     {"name": "Katie", "age": 33, "pet": "Cisco"}] 
} 
""" 

result = json.loads(data) 
df = pd.DataFrame(result['siblings'], columns=['name', 'age']) 
print(df) 

我從「Python for Data Analysis」一書中得到了這個例子,我得到的只是keyerror,我不知道錯誤在哪裏。python KeyError,找不到錯誤

+1

您需要大寫''Siblings''。 –

+0

注意區分大小寫! ''兄弟姐妹''和'兄弟姐妹'不一樣'' – mmenschig

+1

我只是很笨!非常感謝 –

回答

2

可能是最簡單的答案了。注意區分大小寫。

import json 
import pandas as pd 

data = """ 
{ 
    "name": "Wes", 
    "place_lived": ["United Stats", "Spain", "Germany"], 
    "pet": null, 
    "Siblings": [ 
     {"name": "Scott", "age": 25, "pet": "Zuko"}, 
     {"name": "Katie", "age": 33, "pet": "Cisco"}] 
} 
""" 

result = json.loads(data) 
df = pd.DataFrame(result['Siblings'], columns=['name', 'age']) 
print(df)