Simple Steps to Import Kaggle Data Sets into Google Colab

Venkatesh
3 min readApr 21, 2020

Easy steps to download Kaggle data sets into Google Colab

In this tutorial, I show you a simple steps to import Kaggle data sets into google Colab. Kaggle is one of the lead large data sets providers. Google Colab is the online platform suitable for training machine learning models and deep neural network models free of cost without any installation requires. Google Colab is the game changing thing for the people who do not have GPU laptops. Importing Kaggle data sets into Google Colab is not easy. I would like to show you a few simple steps through API.

Firstly create a Kaggle account if do not have already one. Click on your profile picture and again click on my account.

Now scroll down and click on the create new API token then it will download a kaggle.json file and note down the file downloaded path.

Now go to Google Colab, open new python 3 notebook and install dependencies using following command.

!pip install kaggle

In the next cell upload the previously downloaded file into notebook. To upload the file execute the following commands.

from google.colab import filesfiles.upload()

It will prompt a dialog browser like below and choose the kaggle.json file and upload it.

You can see it is successfully uploaded or not in the files section.

If you found kaggle.json file in your files directory then the file is successfully uploaded and execute the following commands

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

Now change the permission

! chmod 600 ~/.kaggle/kaggle.json

Now go to Kaggle website and search for required data set. Let us take a example of amazon fine food reviews.

Open the amazon fine food reviews and click on the 3 dots of right side of the page. In that you can find copy API command and click on it.

Now come back to Colab, in the next cell paste the API command and add exclamation mark at starting of command to execute it. It will download a zip file in files directory

My copied API command is shown below

!kaggle datasets download -d snap/amazon-fine-food-reviews

To unzip the downloaded data set, execute the following commands

from zipfile import ZipFilefile_name = '<data set name>.zip'
with ZipFile(file_name,'r') as zip:
zip.extractall()
print('Done')

Now you can see the extracted files in your files directory.

To read CSV file, execute the following commands

import pandas as pd
Data = pd.read_csv('csv file name.csv')
print(Data.head()

Update

I have found another simpler one than above one. just follow the steps.

!pip install kaggle
api_token ={"username":"UserName","key":"API_key"}
import json
import os
!mkdir /root/.kaggle
!echo '{"username":"UserName","key":"API_key"}'> /root/.kaggle/kaggle.json
with open ('/root/.kaggle/kaggle.json','w') as file:
json.dump(api_token,file)
!kaggle datasets download -d snap/amazon-fine-food-reviews

If you found this helpful just give me Claps. It gives me confidence to write more articles.

--

--