开始使用免费开始使用

列选择:.iloc[] 与按列名

在上一个练习中,您看到如何使用 .loc[].iloc[] 根据索引定位 DataFrame 的特定「行」。结果表明,完成这项任务时 .iloc[] 的速度要快得多(约 2 倍)!

另一项重要任务是更高效地选择 DataFrame 中目标「特征(列)」。本练习将比较以下两种方式:

  • 使用索引定位器 .iloc()
  • 使用列名 二者都能完成相同的工作,但我们关注哪一种在速度上更高效。

在本练习中,您将继续使用存储在 poker_hands 中的扑克牌数据。请先在控制台调用 poker_hands.head(),快速查看该 DataFrame 的结构!

本练习是课程的一部分

使用 pandas 编写高效代码

查看课程

交互式实操练习

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

# Use .iloc to select the first, fourth, fifth, seventh and eighth column and record the times before and after
iloc_start_time = ___
cols = poker_hands.___[___,[0,3,___,___,___]]
iloc_end_time = ___

# Print the time it took
print("Time using .iloc[] : {} sec".format(___ - ___))
编辑并运行代码