
6
import pandas as pd
url = 'https://raw.githubusercontent.com/06Cata/Kaggle_Titanic/main/raw_data/train.csv'
df = pd.read_csv(url)
df.head()

import pandas as pd
import tempfile
import requests
url = 'https://raw.githubusercontent.com/06Cata/Kaggle_Titanic/main/raw_data/train.csv'
response = requests.get(url)
# 數據保存到臨時文件中
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(response.content)
temp_file_path = temp_file.name
df2 = pd.read_csv(train_file_path)
df2

!pip install -U -q PyDrive
import pandas as pd
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# 認證,這裡會跳出google登入頁面,選自己的帳號按繼續
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# google drive 文件夾連結
link = 'https://drive.google.com/file/d/1tPJeYAHyFGhHmdDTSzvuiBSXbVH3uWgM/view?usp=sharing'
# 提取文件夾ID
file_id = link.split('/d/')[1].split('/')[0]
# download
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('train.csv')
df_drive = pd.read_csv('train.csv')
df_drive

用上傳文件的方式
會先跳出【選擇檔案】,直接選本地端的檔案
from google.colab import files
import pandas as pd
import io
uploaded = files.upload()
file_name = 'train.csv'
df3 = pd.read_csv(io.BytesIO(uploaded[file_name]))
df3.head()
