Skip to content

Instantly share code, notes, and snippets.

@GermanCM
Created February 4, 2019 12:34
Show Gist options
  • Save GermanCM/c2faa44f81d7eec267b47b11eabdb559 to your computer and use it in GitHub Desktop.
Save GermanCM/c2faa44f81d7eec267b47b11eabdb559 to your computer and use it in GitHub Desktop.
Read a csv file from google drive nd download it as a dataframe
def loadDataFromDrive(dataLink, fileName):
'''
dataLink: link obtained from the right button option 'get shareable link' in drive
fileName: name of the file in frive
'''
# Code to read csv file into Colaboratory:
get_ipython().system('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)
# get the ID from the data link:
fluff, id = dataLink.split('id=')
downloaded = drive.CreateFile({'id':id})
downloaded.GetContentFile(fileName+'.csv')
import pandas as pd
data_final_obj = pd.read_csv(fileName+'.csv')
return data_final_obj
@GermanCM
Copy link
Author

GermanCM commented Feb 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment