本篇教學Colab,使用三種方法讓Colab讀取CSV檔
Colab是Google提供的免費平台,允許用戶使用Python進行編碼。 Colab本質上是Jupyter筆記本的Google Suite版本。
Colab優於Jupyter的一些優點包括更容易安裝包和共享文本。
github
點擊RAW,複製網址列網址,貼到程式碼中
url = '將網址貼到這'
df1 = pd.read_csv(url)
# Dataset is now stored in a Pandas Dataframe
上傳
from google.colab import files
# 上傳CSV
uploaded = files.upload()
import io
df2 = pd.read_csv(io.BytesIO(uploaded['你的檔案名稱.csv']))
Google Drive
import pandas as pd
# Code to read csv file into Colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
出現提示時,點擊連結進行身分認證,允許訪問你的Google Drive,許可後將驗證碼貼製colab的驗證框中;
完成驗證後,到Google Drive中的CSV文件,右鍵選擇“取得檔案共用連結”。該連結將被複製到剪貼板中。將此連結貼到Colab中的link變數中。
link = 'https://drive.google.com/open?id=1GtzgAplaOjHEgXe2Rc-_pd1Dt_Wxl4Ya'
# The shareable link
fluff, id = link.split('=')
print (id)
# Verify that you have everything after '='
取得Dataframe
downloaded = drive.CreateFile({'id':id})
downloaded.GetContentFile('Filename.csv')
df3 = pd.read_csv('Filename.csv')
# Dataset is now stored in a Pandas Dataframe
想請教說那要如何一次讀取google drive檔案夾裡頭所有的資料呢?有比較快的方式嗎?還是也必須一個一個驗證?謝謝
回覆刪除from google.colab import drive
刪除drive.mount('/content/drive')
# !ls "/content/drive/My Drive"
# !cd "/content/drive/My Drive/@your_folder"
# 類似絕對路徑的操作,可以用迴圈取得所有資料
df = pd.read_csv('/content/drive/My Drive/@your_forlder/@your.csv')
df.head(10)
https://gist.github.com/draguitar/d2845b7f3d86ee86991b86ad2a863442
刪除