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.
Loading Datasets in Google Colab- FAQs
What is the command to read a dataset in Google Colab?
You can use Pandas to read a dataset in Google Colab. For example, to load a CSV file, use the following code:
import pandas as pd
dataset = pd.read_csv('/content/drive/MyDrive/path_to_your_dataset.csv')
How can I upload a dataset in Google Colab?
To upload a dataset in Google Colab, you can either mount Google Drive to access files stored there or use the file upload option provided in Colab, allowing you to upload files directly from your local system.
Can I use a dataset from Google Drive in Colab?
Yes, you can use a dataset from Google Drive in Colab by mounting your Google Drive, which enables you to access and manipulate your datasets seamlessly within your notebook.
Google Colab supports various file formats, including CSV, Excel, and JSON. You can use the corresponding Pandas functions, such as pd.read_excel() or pd.read_json(), to import a dataset in Google Colab.
How do I check if my dataset is successfully loaded in Google Colab?
After loading the dataset, you can use the head() function to display the first few rows of the dataset:
Similar Reads
How to Load a Dataset From the Google Drive to Google Colab
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 advantag
6 min read
How to Import a Quiz from Google Doc into Google Form
Creating quizzes in Google Forms can be time-consuming if you already have the content in a Google Doc. Fortunately, importing a question from a Google Doc into Google Forms can streamline the process, saving you the effort of manually transferring questions. Whether you’re a teacher, a business tra
10 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 get file link from google cloud storage using Node.js ?
To get the signed public link of a file from the Firebase storage, we need a reference to the file in Google Cloud storage. As we just have a path to that file in storage we would first need to create a reference to that object and then get a signed link to that file. Steps to generate a public link
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 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 Convert an Excel Document to a Google Sheet
Are you looking to seamlessly transition your data from Excel to Google Sheets? Converting your spreadsheets can open up a world of collaborative possibilities, making your data accessible from anywhere with an internet connection. In this article, we’ll show you how to convert Excel to Google Sheet
6 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 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 Download Dynamic Files Created During Work on Google Colab?
Answer: To download dynamic files created during work on Google Colab, use the files.download() function after saving the file.To download dynamic files created during work on Google Colab, follow these steps: 1. Save the Dynamic Files: Generate or create the dynamic files during your work in Google
2 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 Download IMAGE From Google Docs (7 Methods)
Downloading an image from Google Docs may not seem straight at first, but it can be easily done with a few simple steps. Imagine working on a collaborative project where you need to save images or download a photo from Google Docs for presentations or reports. While Google Docs doesn’t provide a dir
10 min read
How to Update Files in Google Drive without Changing the Link?
Multiple files with the same name and format can be uploaded on Google Drive, but that does not mean that the new file should replace the old one. After the new file is uploaded, it will get a different shareable link that may lead to a mix-up in case the old one is already sent. What is more, when
3 min read
How to Import All Data From Chrome to Firefox?
Switching browsers is easy when you know how to import data from Chrome to Firefox. Whether you're looking to transfer bookmarks, passwords, or browsing history, Firefox provides a seamless process for migrating all your important data. In this guide, we will show you step-by-step how to transfer Ch
2 min read
How to run Flask App on Google Colab?
Flask is a web framework in python language. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a machine learning webapp. Googl
2 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
How to load a TSV file into a Pandas DataFrame?
In this article, we will discuss how to load a TSV file into a Pandas Dataframe. The idea is extremely simple we only have to first import all the required libraries and then load the data set by using various methods in Python. Dataset Used: data.tsv Using read_csv() to load a TSV file into a Panda
1 min read
How To Convert Image To Text In Google Docs
Google Docs is a free online document editor provided by Google. Google Docs makes it easier to create a document for personal and professional uses with a variety of features. People often take photos of notes on the blackboard a billboard, or from a newspaper for using it later. These text content
9 min read