開始使用免費開始

依列/欄編號做子集

最常見的列子集方式,是用布林條件或索引標籤。 不過,有時候以列編號來擷取也很實用。

這可以用 .iloc[] 完成。和 .loc[] 類似,它可以接受兩個引數,讓你同時依列與欄來建立子集。

已載入 pandas 並命名為 pdtemperatures(沒有索引)可供使用。

本練習屬於課程

使用 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(____)
編輯並執行程式碼