開始使用免費開始

從網路匯入非平面檔案

做得好!你剛剛用 pandaspd.read_csv(),直接把網路上的平面檔案載入成 DataFrame,而不需要先存成本機檔案。這個函式很強大,因為它有親戚可以載入各種類型的檔案,不只限於平面檔。在這個互動練習中,你會使用 pd.read_excel() 來匯入一個 Excel 試算表。

試算表的 URL 為:

'https://assets.datacamp.com/course/importing_data_into_r/latitude.xls'

你的任務是使用 pd.read_excel() 讀入所有工作表,列印工作表名稱,然後列印第一個工作表的前幾列,且要「用工作表名稱而不是索引」來存取。

請注意,pd.read_excel() 的輸出是一個 Python 字典,鍵是工作表名稱,值則是對應的 DataFrame。

本練習屬於課程

Python 資料匯入進階

檢視課程

練習說明

  • 將檔案的 URL 指派給變數 url
  • 使用 pd.read_excel() 讀取 url 指向的檔案為字典 xls;請記得若要匯入所有工作表,需要將 sheet_name 參數設為 None
  • 列印這個 Excel 試算表中的所有工作表名稱;這些會是字典 xls 的鍵。
  • 列印第一個工作表的前幾列,且要「用工作表名稱,而不是工作表的索引」來存取!工作表名稱是 '1700'

動手互動練習

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

# Import package
import pandas as pd

# Assign url of file: url


# Read in all sheets of Excel file: xls


# Print the sheetnames to the shell


# Print the head of the first sheet (using its name, NOT its index)

編輯並執行程式碼