嵌套的电影数据
您一直对电脑里保存已久的一份 movies 数据集很好奇,其中包含多部电影的数据。您想要对其进行分析,但发现它是嵌套的 JSON 格式。
要将其读入 DataFrame,您需要使用刚学到的函数。之后,您会对得到的 DataFrame 进行重塑,以便更容易处理。
名为 movies 的半结构化 JSON 已为您提供。请先在控制台中查看其内容!
本练习是课程的一部分
使用 pandas 重塑数据
练习说明
- 从
pandas中导入json_normalize()函数。 - 规范化
movies中的 JSON。将由嵌套记录生成的名称用下划线分隔。 - 将得到的
movies_normDataFrame 从宽表重塑为长表,使用director和producer列作为唯一索引。将由列movies创建的新变量命名为以features开头、用下划线分隔且后缀为单词的名称。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the json_normalize function
____
# Normalize movies and separate the new columns with an underscore
movies_norm = ____(____, sep=____)
# Reshape using director and producer as index, create movies from column starting from features
movies_long = pd.____(____, stubnames=____,
i=____, j=____,
sep=____, suffix=____)
# Print movies_long
print(movies_long)