2019年7月22日 星期一

[Python]Colab讀取資料三種方法

本篇教學Colab,使用三種方法讓Colab讀取CSV檔 Colab是Google提供的免費平台,允許用戶使用Python進行編碼。 Colab本質上是Jupyter筆記本的Google Suite版本。 Colab優於Jupyter的一些優點包括更容易安裝包和共享文本。


github

點擊RAW,複製網址列網址,貼到程式碼中
  1. url = '將網址貼到這'
  2. df1 = pd.read_csv(url)
  3. # Dataset is now stored in a Pandas Dataframe

上傳


  1. from google.colab import files
  2. # 上傳CSV
  3. uploaded = files.upload()
  1. import io
  2. df2 = pd.read_csv(io.BytesIO(uploaded['你的檔案名稱.csv']))

Google Drive

  1. import pandas as pd
  1. # Code to read csv file into Colaboratory:
  2. !pip install -U -q PyDrive
  3. from pydrive.auth import GoogleAuth
  4. from pydrive.drive import GoogleDrive
  5. from google.colab import auth
  6. from oauth2client.client import GoogleCredentials
  7. # Authenticate and create the PyDrive client.
  8. auth.authenticate_user()
  9. gauth = GoogleAuth()
  10. gauth.credentials = GoogleCredentials.get_application_default()
  11. drive = GoogleDrive(gauth)
出現提示時,點擊連結進行身分認證,允許訪問你的Google Drive,許可後將驗證碼貼製colab的驗證框中; 完成驗證後,到Google Drive中的CSV文件,右鍵選擇“取得檔案共用連結”。該連結將被複製到剪貼板中。將此連結貼到Colab中的link變數中。
  1. link = 'https://drive.google.com/open?id=1GtzgAplaOjHEgXe2Rc-_pd1Dt_Wxl4Ya'
  2. # The shareable link
  3. fluff, id = link.split('=')
  4. print (id)
  5. # Verify that you have everything after '='
取得Dataframe
  1. downloaded = drive.CreateFile({'id':id})
  2. downloaded.GetContentFile('Filename.csv')
  3. df3 = pd.read_csv('Filename.csv')
  4. # Dataset is now stored in a Pandas Dataframe