處理 JSON 的取向(orientation)
JSON 不是表格格式,因此在載入資料時,pandas 會根據其取向做出假設。多數你會遇到的 JSON 取向,pandas 都能自動轉換成 dataframe。
有時候,例如這個經過修改的 Department of Homeless Services 每日報告,資料的取向會不同。為了減小檔案大小,它使用了 split 格式。你會看到用一般方式載入與指定 orient 參數時的差異。try/except 區塊會在載入資料發生錯誤時提醒你。
pandas 已以 pd 載入。
本練習屬於課程
使用 pandas 的精實資料導入
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
try:
# Load the JSON without keyword arguments
df = ____
# Plot total population in shelters over time
df["date_of_census"] = pd.to_datetime(df["date_of_census"])
df.plot(x="date_of_census",
y="total_individuals_in_shelter")
plt.show()
except ValueError:
print("pandas could not parse the JSON.")