How to Mount Google Drive in Google Colab

Unicorn Day
4 min readJun 6, 2024

Google Colab is a fantastic tool for data scientists and machine learning enthusiasts. It’s a free Jupyter notebook environment that runs entirely in the cloud, and it allows you to write and execute code in Python. One of the powerful features of Google Colab is its seamless integration with Google Drive, which lets you easily access and save files to your Drive.

In this article, I’ll walk you through the simple steps to mount your Google Drive in Google Colab, so you can effortlessly use your Drive files in your projects.

Why Mount Google Drive?

Mounting Google Drive in Google Colab allows you to:
- Access large datasets stored in Google Drive without needing to upload them every time.
- Save your work directly to Google Drive for easy access and sharing.
- Load pre-trained models and other assets directly from Google Drive.

Step-by-Step Guide to Mount Google Drive

Step 1: Import the Required Library

The first step is to import the `drive` module from the `google.colab` package. This module provides functions to interact with Google Drive.

# Import the library to mount Google Drive
from google.colab import drive

Step 2: Mount the Drive

Next, use the `mount` function to mount your Google Drive. The `mount` function requires the path where you want to mount the drive, which is usually `/content/drive`.

# Mount the Google Drive at /content/drive
drive.mount('/content/drive')

# Verify by listing the files in the drive
!ls /content/drive/My\ Drive/

Step 3: Authenticate

When you run the above code, you will see a prompt with a link to obtain an authorization code. Follow these steps:
1. Click on the URL provided in the output.
2. Select the Google account you want to use.
3. Allow Google Colab to access your Google Drive.

Step 4: Access Your Drive Files

After mounting the drive, you can access your Google Drive files as if they were on your local file system. For example, to list the files in your Drive, you can use the `ls` command:

# Verify by listing the files in the drive
!ls /content/drive/My\ Drive/

Complete Example

Here is a complete example of how to mount Google Drive and list its contents:

# Import the library to mount Google Drive
from google.colab import drive

# Mount the Google Drive at /content/drive
drive.mount('/content/drive')

# Verify by listing the files in the drive
!ls /content/drive/My\ Drive/

Accessing a CSV File on Google Drive

Once you’ve mounted your Google Drive in Google Colab, you can easily read CSV files using the `pandas` library. Below is a step-by-step guide and a complete example.

Step-by-Step Guide

1. Mount Google Drive: Follow the steps to mount your Google Drive as described earlier.
2. Locate the CSV File: Ensure you know the path to your CSV file in Google Drive.
3. Read the CSV File: Use `pandas` to read the CSV file.

Complete Example

1. Mount Google Drive:

from google.colab import drive
drive.mount('/content/drive')

2. Import the pandas library:

import pandas as pd

3. Read the CSV file: Assume the CSV file is located at `My Drive/data/my_file.csv`.

# Define the file path
file_path = '/content/drive/My Drive/data/Tuition.csv'

# Read the CSV file into a pandas DataFrame
df = pd.read_csv(file_path)

# Display the first few rows of the DataFrame
print(df.head())

Explanation

- Mount the Drive: This step mounts your Google Drive to `/content/drive` in your Colab environment.
- Define the File Path: You need to specify the path to your CSV file in Google Drive. Adjust `file_path` according to your file’s location.
- Read the CSV File: `pd.read_csv(file_path)` reads the CSV file into a pandas DataFrame. The `print(df.head())` command prints the first few rows of the DataFrame to verify that the file was read correctly.

Example Output

If your CSV file looks like this:

               Department                  Major                    Degree  \
0 Police Business/Admin./Mgmt. AA
1 Police Business/Admin./Mgmt. AA
2 Police Business/Admin./Mgmt. AA
3 Health & Human Services Business/Admin./Mgmt. Masters (MA/MS/MPH/etc.)
4 Health & Human Services Other/Misc. Masters (MA/MS/MPH/etc.)

This example demonstrates how to seamlessly access and read a CSV file stored in Google Drive using Google Colab and pandas. You can now manipulate and analyze your data using all the powerful tools provided by pandas and Python.

Conclusion

Mounting Google Drive in Google Colab is a straightforward process that greatly enhances your ability to manage data and projects. By following these simple steps, you can seamlessly integrate Google Drive into your Colab workflow, making it easier to access, share, and save your files.

Whether you’re working on data science projects, machine learning models, or just want to leverage the cloud for your Python programming, Google Colab’s integration with Google Drive is a powerful feature that can save you time and effort.

Happy coding!

— -

I hope you find this guide helpful! If you have any questions or need further assistance, feel free to leave a comment below.

--

--