How to import data into google colab with google drive?

I have data files uploaded to my Google drive. I want to import these files into google colab.

The REST API method and PyDrive method show how to create a new file and upload it to disk and colab. Using this, I cannot figure out how to read data files already present on my disk in my python code.

I am new to this. Can someone help me?

0
source share
2 answers

For a spreadsheet file, the basic idea is to use the gspread and pandas packages to read spreadsheets on Drive and convert them to the pandas dataframe format.

In a Colab laptop:

#install packages !pip install --upgrade -q gspread !pip install gspread-dataframe !pip install pandas #import packages and authorize connection to Google account: import pandas as pd import gspread from gspread_dataframe import get_as_dataframe, set_with_dataframe from google.colab import auth auth.authenticate_user() # verify your account to read files which you have access to. Make sure you have permission to read the file! from oauth2client.client import GoogleCredentials gc = gspread.authorize(GoogleCredentials.get_application_default()) 

Then I know 3 ways to read Google spreadsheets.

By file name:

 spreadsheet = gc.open("goal.csv") # Open file using its name. Use this if the file is already anywhere in your drive sheet = spreadsheet.get_worksheet(0) # 0 means the first sheet in the file df2 = pd.DataFrame(sheet.get_all_records()) df2.head() 

By URL:

  spreadsheet = gc.open_by_url('https://docs.google.com/spreadsheets/d/1LCCzsUTqBEq5pemRNA9EGy62aaeIgye4XxwReYg1Pe4/edit#gid=509368585') # use this when you have the complete url (the edit#gid means permission) sheet = spreadsheet.get_worksheet(0) # 0 means the first sheet in the file df2 = pd.DataFrame(sheet.get_all_records()) df2.head() 

By file / ID key:

 spreadsheet = gc.open_by_key('1vpukIbGZfK1IhCLFalBI3JT3aobySanJysv0k5A4oMg') # use this when you have the key (the string in the url following spreadsheet/d/) sheet = spreadsheet.get_worksheet(0) # 0 means the first sheet in the file df2 = pd.DataFrame(sheet.get_all_records()) df2.head() 

I shared the code above on a Colab laptop: https://drive.google.com/file/d/1cvur-jpIpoEN3vAO8Fd_yVAT5Qgbr4GV/view?usp=sharing

+2
source

!) Set your data in a public form, then for public tables:

 from StringIO import StringIO # got moved to io in python3. import requests r = requests.get('https://docs.google.com/spreadsheet/ccc? key=0Ak1ecr7i0wotdGJmTURJRnZLYlV3M2daNTRubTdwTXc&output=csv') data = r.content In [10]: df = pd.read_csv(StringIO(data), index_col=0,parse_dates= ['Quradate']) In [11]: df.head() 

Read more here: Getting a Google CSV spreadsheet in a Pandas Dataframe

If the private data looks the same, but you have to do some kind of out gymnastics ...

+1
source

Source: https://habr.com/ru/post/1274814/


All Articles