2019年7月22日 星期一

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

本篇教學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