從網路開啟並讀取平面檔案
你剛剛從網路匯入了一個檔案、將它儲存到本機,並載入到一個 DataFrame。如果你只想直接把網路上的檔案載入到 DataFrame,而不先存到本機,也可以用 pandas 輕鬆完成。特別是,你可以使用 pd.read_csv(),把 URL 放在第一個引數,分隔符 sep 放在第二個引數。
檔案的 URL 再提供一次:
'https://assets.datacamp.com/production/course_1606/datasets/winequality-red.csv'
本練習屬於課程
Python 資料匯入進階
練習說明
- 將檔案的 URL 指派給變數
url。 - 使用
pd.read_csv()將檔案讀入為 DataFramedf,記得檔案中的分隔符是';'。 - 列印 DataFrame
df的前幾筆(head)。 - 執行其餘程式碼,以對 DataFrame
df中第一個特徵繪製長條圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import packages
import matplotlib.pyplot as plt
import pandas as pd
# Assign url of file: url
# Read file into a DataFrame: df
# Print the head of the DataFrame
print(____)
# Plot first column of df
df.iloc[:, 0].hist()
plt.xlabel('fixed acidity (g(tartaric acid)/dm$^3$)')
plt.ylabel('count')
plt.show()