How to Load a Dataset From the Google Drive to Google Colab
Last Updated :
08 Oct, 2024
Google Colab (short for Collaboratory) is a powerful platform that allows users to code in Python using Jupyter Notebook in the cloud. This free service provided by Google enables users to easily and effectively load a dataset in Google Colab without the need for local resources. One of the advantages of Google Colab is that it offers access to GPU and TPU, allowing for faster computations similar to what you would find in a local Jupyter Notebook. Additionally, Colab facilitates quick installation and real-time sharing of Notebooks among users, making it a great choice for collaborative projects.
Step-by-Step Guide to Loading Datasets from Google Drive
To read a dataset in Google Colab from an external source, such as Google Drive, you will need to write a few lines of code. Here’s a step-by-step guide on how to upload a dataset in Google Colab from Drive:
Step 1: Mount Google Drive
Using the built-in code cell in Google Colab, you can load a dataset in Google Colab by mounting your Google Drive. This process grants you access to the documents and folders in your Google Drive account. To mount your Google Drive, use the following code:
from google.colab import drive
drive.mount("/content/drive")
Using the mount() function in Google Colab allows any code in the notebook to access any file in Google Drive. This is a crucial step for users wanting to import a dataset in Google Colab directly from their Google Drive, as it enables seamless interaction with the files stored there. Once the drive is mounted, you can easily navigate to your datasets, making it simple to read datasets in Google Colab for your machine learning projects.
Step 2: Authorisation Access
When you run the code cell to load a dataset in Google Colab, you will be prompted with a request for permission to grant Google Colab access to your Google Drive files. This is an essential step for how to upload a dataset in Google Colab from Drive.
.jpg)
After allowing the permission, you will be redirected to a page displaying your email ID access. Following this, an authentication key will be provided, which you need to input into the prompt in Google Colab. This process is crucial for ensuring that you can import a dataset in Google Colab securely and seamlessly access the files stored in your Google Drive.

Step 3: Google Drive Mounted
After completing Step 2, your Google Drive will be mounted, as illustrated in the image below. At this point, you can easily read your dataset file from Google Drive.

However, before proceeding, it's essential to check your current working directory using the command:
!pwd
pwd stands for print working directory. It is a command that is used in Unix-like operating systems, such as Linux and macOS, to display the current working directory, or the location or working directory in the file system that you are now using in the command line interface.
When you run the pwd command, the entire path to the current directory will be printed to the terminal. This is useful when exploring directories and interacting with files and directories via the command line because it helps you remember where you are in the file system.
!pwd
As shown in the image above, after executing the command in the colab cell, it is said that the current working directory is /content and the drive is mounted at /content/drive. Therefore, one must start from /content/drive, which is the drive, in order to access the dataset.
Step 4: Accessing the dataset
Once step 3 is completed, you can easily navigate to the folder where your dataset is stored. for this, a command will be used called
!ls
ls is a command commonly used in Unix-like operating systems, including Linux and macOS, for listing the files and directories in the current directory (or a specified directory). It provides a way to view the contents of a directory from the command line.
For example, we will use a sales.csv to show the steps:
!ls /content/drive/MyDrive/sales.csv
.jpg)
Here, the sales.csv dataset is located in the folder named MyDrive. By using this command, you can effectively manage your datasets and ensure that you can seamlessly load a dataset in Google Colab for your machine learning tasks.
Step 5: Loading Dataset
Now, depending on the structure of your dataset, you can load a dataset in Google Colab using Python libraries like Pandas for tabular data or NumPy for arrays.
import pandas as pd
df=pd.read_csv("/content/drive/MyDrive/sales.csv")
pd.read_csv is a function provided by the popular Python library called Pandas. Pandas is commonly used for data manipulation and analysis in data science and data engineering tasks. The pd.read_csv function specifically is used to read data from CSV (Comma-Separated Values) files into a Pandas DataFrame.
Finally, you can now work with the dataset in your Google Colab, similar to as you would have done in any other Python environment.
By following these steps, you can effectively add a dataset in Google Colab from Google Drive and begin working with it. This process allows you to easily use a dataset from Google Drive in Colab and facilitates smooth data analysis and model training.
Conclusion
In this article, we discussed how to load a dataset in Google Colab from Google Drive, emphasizing the platform's benefits for machine learning. We covered the steps to mount Google Drive, locate your dataset, and use commands like !ls
to access files. By learning how to read a dataset in Google Colab and import a dataset in Google Colab, you can efficiently manage your data for analysis and model training. With these skills, you are well-prepared to upload a dataset in Google Colab and effectively use a dataset from Google Drive in Colab for your projects.
Similar Reads
How to Password Protect a Google Drive Folder: Comprehensive Guide Google Drive is a popular cloud storage service that allows users to store, share, and access their files from anywhere. However, you may want to secure some of your sensitive files by password-protecting them. While Google Drive doesnât offer a built-in password protection feature for folders, ther
9 min read
Delete a locally Uploaded File on Google Colab In this article, we will learn to delete locally uploaded folders in our Google Colab notebook. You can also refer to the video solution for this end which is attached at the end of this article. Delete Local Uploaded file on Google ColabPrerequisite for the task - A Google Account. Refer to this ar
2 min read
How to Share files on Google Drive In today's fast-paced digital world, efficient file sharing is crucial for smooth collaboration and productivity. Whether you're working on a team project, sharing important documents with colleagues, or simply distributing files to friends and family, Google Drive offers a seamless and effective so
4 min read
How to load CSV data from the local to Snowflake? In today's data-driven world, efficient data management and transfer are critical for business success. Snowflake, a powerful cloud-based data warehousing solution, allows organizations to store and analyze vast amounts of data seamlessly. In this article, we will cover everything from the basics to
4 min read
How to Upload Project on GitHub from Google Colab? If you want to create a machine learning model but say you donât have a computer that can take the workload, Google Colab is an open-source platform for you. Even if you have a GPU or a good computer creating a local environment with anaconda and installing packages and resolving installation issues
2 min read
How to Open a CSV File in Google Sheets Ever received a data dump in a mysterious file format called CSV? Don't worry, it's not an alien language! CSV stands for "Comma-Separated Values," and it's a common way to store data in rows and columns. But how do you unlock this treasure trove of information? Fear not, data warrior! This guide wi
5 min read
How To Access Google Docs From A Non Gmail Account Google Docs is a platform where you can log in when you have a Google Gmail account, but you can also log in with a non-Gmail account into Google Docs. To access Google Docs with your non-Gmail account, you must make a Google account first with your existing Gmail. In this article, we will explore h
4 min read
How to load a huggingface dataset from local path? Hugging Face datasets â a powerful library that simplifies the process of loading and managing datasets for machine learning tasks. Loading a Hugging Face dataset from a local path can be done using several methods, depending on the structure and format of your dataset. In this comprehensive guide,
6 min read
How to upload folders to Google Colab? Google Colab is a service provided by Google for a lot of researchers and developers around the globe. It is a Jupyter Notebook-like environment in one single place without any prerequisites. It is free to use with a limited number of computer resources and engines including free access to GPUs i.e.
4 min read
How to Use Google Drive for Desktop To easily manage and share content across all your devices and the cloud, use Googleâs desktop sync client. With Drive for Desktop, you can access your Drive files and folders directly from Windows File Explorer or macOS Finder on your computer. When you edit, delete, or move a file on the cloud, th
8 min read