Different Ways to Connect Google Drive to a Google Colab Notebook! (Part 1)

Collaboratory ways of working with data using Google Colab

Sebastian Carmona A.
Towards Data Science

--

Photo by Joshua Aragon on Unsplash

Nowadays, we live in a collaborative ambient where our projects are shared with our co-workers, for review or collaboration. Also, we are using our cloud storage more and more every time, and if it has to be for any data purposes (.csv, .json, .xlsx files, etc.) it's better, most of all if we are in a collaborative ambient, anyone that has access to this information can collaborate in our projects or share more data.

So, in this pt.1 of the article, I will like to share with you some tricks I have learned in my data path and collaborative working!

Connecting Specific folders of your Google Drive

Let us say we want to analyze some data in a Google Colab notebook, some .csv files that have been shared with us by Google Drive recently. But, we don't want to download all that data from Google Drive and then upload it to our notebook, this will take time, and maybe those .csv files could change in the future and we will have to make the whole process again.

For this, Google Colab has a way of mounting our Google Drive using the following code:

After running this code, some messages will be prompted:

Connect to Google Drive — Image by Author
Select the Google Drive Account you want to mount — Image by Author
Select “Allow” and that’s it. — Image by Author

After mounting your Google Drive, you can explore all your Google Drive folders and navigate to the folder where you want to read and analyze the files that have been shared. For this, you need to navigate to your home directory of your Google Drive using the following code:

%cd gdrive/MyDrive

Finally, just continue navigating through your folders and files using %cd.

This is the best option I have found if you want to read a lot of files from a single folder.

I usually use the following piece of code to read and concat all files in a single data frame (only if all files have the same schema; the same number of columns and names):

You can check my bikes notebook where I used these pieces of code to mount my Google Drive, navigate into the folder where all my .csv files are hosted. Then I read, clean, processed, transform and concat all those files into a single data frame.

Saving files in the same folder you access

After you had navigated to the folder where you wanted and had all the data you wanted to analyze, you can also save your final result in that same folder or navigate to any other folder in your drive and save the data using the following code for saving .csv files:

df.to_csv("name_file.csv")

Or any other piece of code that saves your data into the type of file that you wanted to be saved.

Uploading specific files from Google Drive to my Google Colab Notebook

Maybe sometimes we only need/want to upload to our runtime/kernel/ambient single files from our Google Drive and we don't want to mount our whole Drive.

We could do it by just downloading our file into Google Colab using the following code:

First, we will need to import the necessary libraries:

Then you can run this next code:

Before running the code above you will need to find your file id first, for this, go to your file in your Google Drive and right-click it to find the file link:

Click on “Get link” — Image by Author

As soon as you get the link you will see something like this:

https://drive.google.com/file/d/{"ID"}/view?usp=sharing
Red box = file id — Image by Author

That whole string with numbers and letters in the red box will be your id that will need to be inserted in this part of the code:

file_id = '{insert your id here}'

Then a message will be prompted:

Click on the link and follow the steps — Image by Author

After you click the link, it will ask you to sign in to your google account and give access to google colab to access your google drive, at the end it will give you a code, copy and paste it on “Enter verification code:

Copy-paste the code and insert it into the input box — Image by Author

Finally, you can do whatever you want with these files.

Conclusion

I showed you 2 different ways to upload or access files from your Google Drive by your Google Colab.

  1. Mounting your Google Drive into your Google Colab
    - You can save files as well into your Google Drive by navigating through folders.

Pros:

  • You mount your whole Google Drive into your Google Colab notebook.
  • You can navigate through folders in your Google Drive in case your project requires multiple files from different locations.

Cons:

  • Any file that you save will be saved in the exact folder you are in, so if you are not exactly sure where you are at inside your Google Drive will be good to run the following expression before saving:
%pwd
  • Sometimes if you aren’t absolutely sure where you are, the saving of files could end up a mess inside your Google Drive and then you will need to organize everything by yourself.

2. Uploading files without needing to mount your whole Google Drive.

Pros:

  • You will not need to mount your whole drive, so you will always be in the same path/directory inside your Google Colab runtime.
  • Files saved will be saved into your runtime and not into your Google Drive, this will be a Pro in case you don't want to save files into your Google Drive or don’t need to navigate through folders, you can download the file into your Pc whenever you want after finishing your project.

Cons:

  • It's not optimal for multiple files, you will need to download them into your runtime (running the same code) multiple times, each time for a different file.

Finally, this is the Pt.1 of a series of Google Drive and Google Colab connections that I will be writing about. Expecting this helps in your collaboratory environment!

Any feedback you have I will be glad to know! If you have any other new topic that has to be with this and wants to know how to do it, just let me know, and will write about it!

Photo by Hanny Naibaho on Unsplash

--

--