开始使用免费开始使用

按行/列编号取子集

我们之前介绍的筛选行的常见方法有两种:使用布尔条件,或使用索引标签。不过,有时按行号取子集也很有用。

可以使用 .iloc[] 来完成。与 .loc[] 类似,它可以接收两个参数,让您同时按行和列取子集。

已将 pandaspd 导入。提供了未设置索引的 temperatures 数据。

本练习是课程的一部分

使用 pandas 进行数据处理

查看课程

练习说明

temperatures 上使用 .iloc[] 取子集。

  • 取第 23 行、第 2 列(索引位置分别为 22 和 1)。
  • 取前 5 行(索引位置 0 到 5)。
  • 取所有行,第 3 和第 4 列(索引位置 2 到 4)。
  • 取前 5 行,第 3 和第 4 列。

交互式实操练习

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

# Get 23rd row, 2nd column (index 22, 1)
print(____)

# Use slicing to get the first 5 rows
print(____)

# Use slicing to get columns 3 to 4
print(____)

# Use slicing in both directions at once
print(____)
编辑并运行代码