Created
February 4, 2019 12:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more info: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=zU5b6dlRwUQk