The Easiest Ways to Download Data from Kaggle into Google Colab. You Must Know!!!

Karim Hasibuan
4 min readApr 9, 2023

--

Hello friends, in this article I want to share information about The Easiest Ways to Download Data from Kaggle into Google Colab that you need to know! 😡

So, I decided to write this article because when I started the data project, I always forgot how to download data from Kaggle directly into Google Colab so I had to look for previous tutorials that I had read. Then, for this time when I forget, I will open my own writing. There is no particular reason other than that.

Okay, before getting into the how-to, I want to make sure that you already know and have a Kaggle and Google Colab account. If you don’t, please create one first. And if you do, let’s get straight to the steps without further ado!

Downloading Kaggle Dataset in Google Colab

For the first step, make sure you already have a Kaggle account and are logged in. Then, go to the Account menu and scroll down a bit until you find the Create New API Token button. It can be seen as the following picture.

Just click the button, then a file named kaggle.json will be downloaded automatically. This file contains the profile API information of your account. Through this file we can access Kaggle through Google Colab.

Second step, let’s go to our Google Colab. Please write the following code in the New Cell of our worksheet.

!pip install kaggle

Run the code above, it will result as shown the image below.

In the next new cell type the code below:

from google.colab import files
files.upload()

Run the code above, it will display a mini pop-up window to select which file you want to upload. Please select and upload the kaggle.json file that you downloaded earlier. The result will look like the following image.

On the third step, write and run the following code in the new Cell.

!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json

No output is displayed directly when running the code above. However, the code above creates a .kaggle folder, moves the kaggle.json file to .kaggle, and grants access permissions to the file.

The fourth step, we return to the Kaggle page. There, please search for the dataset you want to download. Here I will download the fer2013 dataset. Then, press the three dots on the right and click Copy API Command. This will make you have the code to download the dataset.

Then, go back to Google Colab and paste the API Command that was copied earlier in a new cell under the cell that we last ran in this tutorial.

!kaggle datasets download -d msambare/fer2013

Don’t forget to add the ! sign at the beginning to run the code successfully. The code above will download the dataset that we have selected earlier. The output of the above code can look like the following image.

Once the download is complete. Then a .zip file will be saved in our Google Colab worksheet. To be able to use it, you can extract it first. You can use the following code.

import zipfile
zip_ref = zipfile.ZipFile('fer2013.zip', 'r')
zip_ref.extractall('files')
zip_ref.close()

In the fer2013.zip section, please match the .zip file name of the dataset you have downloaded.

Okay, it’s finally done.

Actually this method is very short and easy and the process is also fast. It’s just that I explained in detail so that you can understand it so that it looks longer. And this method is what I usually use too.

Okay, that’s The Easiest Ways to Download Data from Kaggle into Google Colab.

Good Luck and Happy work 😎

--

--

Karim Hasibuan
0 Followers

I am a student who loves Web Development and Data Science.