How to Mount Google Drive in Google Colab

Posted on

This article will inform some alternative to mount Google Drive in Google Colab. First of all, the definition of Google Colab exist in this link. Colaboratory, or “Colab” for short, is a product from Google Research. Colab allows anybody to write and execute arbitrary python code through the browser, and is especially well suited to machine learning, data analysis and education. Actually in the process of data analysis, sometimes there is a need to pull the data from a CSV file. That is where the Google Drive’s role appear. In order to be able to place the CSV file in a online storage, Google Drive is the best alternative to colaborate with Google Colab. The Google Colab will mount the Google Drive and then read the file. The process for mounting the Google Drive in Google Colab is simple. The following is the script for mounting the Google Drive :

from google.colab import drive 
drive.mount('/content/gdrive', force_remount=True)

The following is the output of the command execution above. And after executing it in Google Colab, there will be a text field input with an information label consisting of a link for requesting a code for approval on mounting the Google Drive. It appears as on the following image :

How to Mount Google Drive in Google Colab
How to Mount Google Drive in Google Colab

The following is the execution of the link that will generate a code. But before generating the code, there will be a request to log in first to the gmail account. After logging in successfully, the following page will appear :

How to Mount Google Drive in Google Colab
How to Mount Google Drive in Google Colab

Click the button with the label ‘Allow’. It will redirect the page to the following page with the code available for authorization to access the Google Drive from Google Colab :

How to Mount Google Drive in Google Colab
How to Mount Google Drive in Google Colab

Just copy the code and paste it in the Google Colab as follows :

How to Mount Google Drive in Google Colab
How to Mount Google Drive in Google Colab

Don’t forget to enter after, if the authorization process using the code is a success, the following image will apear :

How to Mount Google Drive in Google Colab
How to Mount Google Drive in Google Colab

After successfully mounting the Google Drive, just execute the following command to read the CSV file from the Google Drive :

os.chdir("/content/gdrive/My Drive/Colab Notebooks")
df=pd.read_csv("file_csv.csv")
print("Row Size : ", df.shape[0])
print("Column Size : ", df.shape[1])
print("Column Type Information")
df.info()

The output will be in the following :

Row Size :  10000
Column Size :  40
Column Type Information
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 40 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   Unnamed: 0           10000 non-null  int64  
...
dtypes: float64(2), int64(11), object(27)
memory usage: 3.1+ MB

As in the above output command execution, it display the total row, total column and also column information of the DataFrame variable. The process for reading CSV file from the Google Drive is a success.

Leave a Reply