從網路匯入平面檔案:換你來試!
你即將從網路匯入第一個檔案!這次要匯入的平面檔案是加州大學爾灣分校的 Machine Learning repository 中的 'winequality-red.csv'。這個檔案包含紅酒的理化性質表格資料,例如 pH、酒精含量與檸檬酸含量,並附有酒款品質評分。
該檔案的網址為:
'https://assets.datacamp.com/production/course_1606/datasets/winequality-red.csv'
匯入後,你會檢查工作目錄以確認檔案已存在,接著將它載入為一個 pandas 的 DataFrame。
本練習屬於課程
Python 資料匯入進階
練習說明
- 從子套件
urllib.request匯入函式urlretrieve。 - 將檔案的網址指定給變數
url。 - 使用
urlretrieve()函式,將檔案在本機儲存為'winequality-red.csv'。 - 執行其餘程式碼,將
'winequality-red.csv'載入為 pandas 的 DataFrame,並在殼層印出其前幾列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import package
from ____ import ____
# Import pandas
import pandas as pd
# Assign url of file: url
# Save file locally
# Read file into a DataFrame and print its head
df = pd.read_csv('winequality-red.csv', sep=';')
print(df.head())