Google Drive CSV file link to Pandas DataFrame

How to read in a csv file from Google Drive to a Pandas DataFrame:

url = ' google drive sharing link'
path = ''+url.split('/')[-2]
df = pd.read_csv(path)

Show DataFrame output on Streamlit app:


If you run into any issues, try making the CSV file link public on Google Drive. This could be a potential workaround for uploading large files.



Works great, any chance there is a way to upload files in a similar way (without google API), overwriting the file? I would like to read the file, append it and the save it back to google drive and still use the same URL.

Thanks in advance!

since the suggested method seems to be unreliable I ended up using a google service account like so

from pandas import read_csv
from pandas import DataFrame
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from io import BytesIO

def google_drive(id:str) -> DataFrame:
    SCOPES = [""]
    creds = Credentials.from_service_account_file(
    service = build(
    request = service.files().get_media(fileId=id)
    file = BytesIO()
    downloader = MediaIoBaseDownload(file,request)
    done = False
    while done is False: _,done = downloader.next_chunk()
    return read_csv(file)
1 Like

Great code, thanks for sharing!