How to import Kaggle Datasets directly to the Google Colab

Mohamed Illiyas
3 min readJul 1, 2021

--

3 steps to import the Kaggle dataset into your colab notebook

Fetching data from Kaggle To Colab

Everybody in the Datascience and Machine Learning Field would have come across the word Kaggle. Here is a short intro about it who didn't hear the word “Kaggle”.

Kaggle is a subsidiary of Google. It is an online platform where many students and enthusiasts actively practice various data science methodologies, Machine learning algorithms, etc. It has a huge set of public datasets, Notebooks of many data scientists, solutions to various problem statements, blogs etc. It also hosts various competitions with or without prize money. We can also work with the team to solve data science challenges. Explore kaggle

Google Colab(collaboratory) is a product of Google research. Anybody can write, execute python code through the browser. It is well suited for machine learning, data analysis, and data science. It also gives access to free GPU and TPU. Colab

Now let us see how we can extract the dataset from Kaggle to Google colab.

Create an account in Google and Kaggle if you don't have before

1. Creating API Token in Kaggle

→Go to Kaggle profile

Click Account

→ Go to Account

→ Scroll down, and you will see the API section.

Click Create New API Token

→ Click Create New API Token and then a JSON file will get downloaded into your PC.

→ If you’ve already created any API Token, you can click Expire API Token and create another new API Token.

2. Running commands in Google Colab

Now run the following commands on the new Notebook of the colab project.

i) Pip Install

! pip install -q kaggle

ii) Uploading JSON file

from google.colab import filesfiles.upload()

→ Now upload the JSON file which we have downloaded from Kaggle(API)

Choose Files

iii) Make a Directory and copy the kaggle.json file to the directory

! mkdir ~/.kaggle! cp kaggle.json ~/.kaggle/

iv) Changing Permission

! chmod 600 ~/.kaggle/kaggle.json

Permissions of 600 mean that the owner has full read and write access to the file.

3. Download Dataset from Kaggle through API command

→Now go to the dataset in Kaggle. In the right corner option, you can find the Copy API command. Click to copy that.

→Now paste the command in google colab cell. Don't forget to add the “ ! “ exclamatory mark at the beginning of the command.

The exclamation mark is used for executing commands from the underlying operating system.

Now you can check whether it is downloaded or not by the “ls” command

ls

4) Unzip the Dataset and Remove the Zip file

Now unzip the file by using this command. Replace Your_Zip_File_Name with your zip file name. After executing this command it will unzip and extract the dataset and remove the zip file automatically.

! unzip Your_Zip_File_Name.zip && rm Your_Zip_File_Name.zip

Now we have downloaded the dataset.

Thanks!

--

--