Skip to Main Content
Python程序设计:人工智能案例实践
book

Python程序设计:人工智能案例实践

by 保罗 戴特尔, 哈维 戴特尔
August 2021
Intermediate to advanced content levelIntermediate to advanced
450 pages
13h 28m
Chinese
Pearson
Content preview from Python程序设计:人工智能案例实践

7.14.2 DataFrame

DataFrame是增强的二维数组。与Series类似,DataFrame可以具有自定义的行和列索引,并提供额外的操作和功能,使其能更方便地执行许多面向数据科学的任务。DataFrame还支持缺失数据情况下的处理。DataFrame中的每一列都是一个Series。代表每个列的Series可能包含不同的元素类型,在我们讨论将数据集加载到DataFrame时,会看到这一点。

从字典中创建一个DataFrame

让我们从字典中创建一个DataFrame,它表示学生在三次考试中的成绩:

pandas以表格格式显示DataFrame,其中索引在索引列中左对齐,其余列的值右对齐。字典的键变成列名,而与每个键相关联的值成为对应列中的元素值。稍后,我们将展示如何“翻转”行和列。默认情况下,行索引是从0开始自动生成的整数。

使用index属性自定义DataFrame的索引

在创建DataFrame时,可以使用index关键字参数指定自定义索引,如下所示:

使用index属性将DataFrame的索引从连续整数更改为标签:

在指定索引时,必须提供元素数等于DataFrame中的行数的一维合集;否则,将引发ValueErrorSeries还提供了一个index ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Python算法交易实战

Python算法交易实战

Posts & Telecom Press, Sebastien Donadio
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111678458