Colab實作: 如何讀取雲端硬碟資料?
在colab進行機器學習開發時,需要讀取資料,colab的功能是可以把資料放在google雲端硬碟中,再透過colab進行讀取。今天主要是分享如何在colab上讀取雲端硬碟資料。
在google雲端硬碟上,我開啟了名為”資料”的資料夾,在裡面上傳了檔名為apx_p_488的csv檔案,這是一個政府公開的空氣品質資料。

接下來,會在colab上面,運行以下程式碼。
第一個是輸入google drive的模組
from google.colab import drive
以及輸入處理資料的pandas模組
import pandas as pd
掛載google雲端硬碟
drive.mount('/content/drive')
定義檔案的路徑,其中My Drive這邊代表的是我的雲端硬碟路徑,意思是一開始進到google雲端硬碟的路徑,在這個路徑上,我有開啟名為”資料”的資料夾,在資料夾裡有aqx_p_488.csv的檔案。
file_path = '/content/drive/My Drive/資料/aqx_p_488.csv'
在透過pandas的read csv功能,可讀取csv資料
df = pd.read_csv(file_path)
再列出檔案的前五筆資料
print(df.head())
以下為完整程式碼
from google.colab import drive
import pandas as pd
drive.mount('/content/drive')
file_path = '/content/drive/My Drive/資料/aqx_p_488.csv'
df = pd.read_csv(file_path)
print(df.head())
執行此程式碼後,會出現要允許筆記本存取google雲端硬碟,此時就點連線至google雲端硬碟

會出現以下畫面,都點選繼續


就可以在下面出現讀取成功的結果,就會在程式碼下面出現csv檔的內容。
