開始使用免費開始

自訂試算表匯入

在這裡,你會剖析試算表,並使用額外引數來略過列、重新命名欄位,以及只選取特定欄位。

試算表 'battledeath.xlsx' 已經載入為 xls

和先前一樣,你會使用 parse() 方法。不過這次你會加入額外的引數 skiprowsnamesusecols。它們分別用來略過資料列、指定欄位名稱,以及指定要剖析哪些欄位。這些引數都可以指定為清單(list),裡面包含適當的列號、字串與欄號。

本練習屬於課程

Python 資料匯入入門

檢視課程

練習說明

  • 以索引剖析第一個工作表。同時略過第一列資料,並使用引數 names 將欄位命名為 'Country''AAM due to War (2002)'。傳給 skiprowsnames 的值都必須是 list 型別。
  • 以索引剖析第二個工作表。同時只用 usecols 參數剖析第一個欄位、略過第一列,並將欄位重新命名為 'Country'。傳給 usecols 的引數也必須是 list 型別。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Parse the first sheet and rename the columns: df1
df1 = xls.parse(____, skiprows=____, names=____)

# Print the head of the DataFrame df1
print(df1.head())

# Parse the first column of the second sheet and rename the column: df2
df2 = xls.parse(____, usecols=____, skiprows=____, names=____)

# Print the head of the DataFrame df2
print(df2.head())
編輯並執行程式碼