开始使用免费开始使用

开读时间,Katniss!

一天的工作快要结束了。不过在这之前,您想做个有趣的小分析。这次您将分析另一个图书数据集,内容来自《饥饿游戏》系列。

您在变形前已经查看过 books_hunger 数据集,但发现有些不对。该 DataFrame 的索引包含书名。您知道这种格式无法直接重塑。否则会丢失宝贵的信息——书名。因此,在转换 DataFrame 之前,您需要先做一些调整。

books_hunger 数据集已为您准备好。它包含 title,以及每本书的 languagepublication datepublication numberpage number 等信息。

本练习是课程的一部分

使用 pandas 重塑数据

查看课程

练习说明

  • 修改 books_hunger DataFrame,重置索引且不要丢弃原索引。
  • books_hunger 从宽表重塑为长表。使用 titlelanguage 列作为唯一索引。将由以 publicationpage 开头的列生成的新变量命名为 feature。这些列以空格分隔,且以一个单词结尾。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Modify books_hunger by resetting the index without dropping it
books_hunger.____(____=____, inplace=____)

# Reshape using title and language as index, feature as new name, publication and page as prefix separated by space and ending in a word
publication_features = pd.wide_to_long(____, 
                                       ____=____, 
                                       ____=____, 
                                       ____=____, 
                                       ____=____, 
                                       ____=____)

# Print publication_features
print(publication_features)
编辑并运行代码