How Can We Upload Data to Google Colab?

The most popular ways of uploading CSV files on the Google Colab notebook.

image

We can import data in Google Colab notebook using Github, Google Drive, and also from your local system. Google Colab is a platform that enables users to train their ML, and DL models on google cloud using free resources like CPU, GPU, and TPU written in python. Google Colab uses a Jupyter notebook-like environment referred to as Colab Notebook. Google Colab provides lots of functionality like easy installation of python packages, code snippets, easy sharing of notebooks among users, easy download of Colab Notebook, and importing CSV files for Exploratory Data Analysis (EDA) and model training tasks.

In this article, we will discuss the methods that are available for uploading and importing the dataset into the Colab environment. Google Colab easily integrates with the other very commonly used services like Github and Google Drive.

Here there are the three most popular methods to upload the CSV files on the Colab notebook-

  1. Using Link: This is the easiest method to import a dataset having a files size of less than 25MB using a GitHub URL into Google Colab. Pandas library allows you to load any CSV using a URL. First, click on the dataset that is present in your repository, then click on View Raw. Copy the link to the raw dataset and store it as a string variable called url in Colab as shown below (a cleaner method but it’s not necessary). The last step is to load the url into pandas read_csv() to get the data frame.

    import pandas as pd
    url = "dataset path"
    pd.read_csv(url)
    
  2. Using Google Drive: To upload a dataset on the Google Colab from google drive, you need to upload it first on google drive. For this, you need to select ‘My Drive’ first, then click on ‘New’ then click on ‘Folder’. Then it will prompt you to enter the folder name as shown in the below image. Here, I have given ‘Datasets’ in the field of the name of the folder.

image

Then click on the ‘Create’ button, It will make a Datasets folder as shown in the below image.

image

Now inside this folder, you need to upload a CSV file. For this, select the Datasets folder, then click on the ‘New’ button, then click on upload file and browse the file from your local system, then press the ‘Open’ button to upload files. After uploading files, your file will be shown in the google drive like this.

image

Now the next step is to import the CSV file from the Google drive to the Google Colab notebook. Run these two lines of code in the Google Colab notebook to mount the Google drive inside Google Colab. On execution of these two lines, Google will ask you to grant permission for accessing the drive. After giving permission, it will mount your drive inside Google Colab. Now you can access google drive by clicking on the ‘folder’ icon in the left bar.

image

Now right click on the zomato.csv file and select the ‘copy path’ option. Now load this CSV using pandas.read_csv(“copied_path”).

image

3. From Local System: Google Colab has its own google.colab library which provides a facility to upload files from your local system to the Google Colab. Using these 2 lines of code, you can easily upload any type of file on Google Colab.

from google.colab import files
uploades = files.upload()
#df = pd.read_csv("zomato.csv", encoding = "latin-1")

Result of running the above code:

image

In this article, we have seen the most popular methods of uploading CSV files on the Google Colab notebook. Thanks for reading.

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics