处理 JSON 的取向
JSON 不是表格格式,因此在加载数据时,pandas 会根据取向做出默认假设。大多数您遇到的 JSON 数据,其取向都能被 pandas 自动转换为 dataframe。
有时(例如这个经过修改的 Department of Homeless Services Daily Report),数据的取向会不同。为了减小文件大小,它使用了 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.")