How to import Kaggle data in Google Colab

Siddhartha
ML Book
Published in
3 min readApr 5, 2021

--

Why do we need to use Kaggle dataset in google Colab when we can train model in Kaggle itself?

  1. To use GPU and TPU of Colab if we have completely used Kaggle quota.
  2. To use interactive IDE of Colab where we have excellent code auto-completion, latest versions of Tensorflow and other frameworks.
  3. To run many training processing as we can use only a limited number of notebooks in Kaggle.

Steps:

you can watch this video for proper explanation or read this full article

  1. Create Kaggle API key:

Go to Kaggle account section and click on create New API Token

it will download one json file kaggle.json which contains you api key, don’t share this file with anyone.

2. Upload kaggle.json in Google Drive:

In google drive create one folder named Kaggle in My Drive, inside that Kaggle folder upload kaggle.json file

3. Download Kaggle data in drive and use in colab:

in colab run following code

from google.colab import drive
drive.mount('/content/gdrive')

this will connect your colab with drive, to do that a pop-up will come and it will ask some permission, allow those permissions.

then run

import os
os.environ["KAGGLE_CONFIG_DIR"] = '/content/gdrive/MyDrive/Kaggle'

it will set Kaggle configure directory to your Kaggle folder in drive

Now go to Kaggle directory

%cd gdrive/MyDrive/Kaggle

Go to the kaggle dataset and copy kaggle api command

for example titanic data api

run following code

!kaggle competitions download -c titanic

it will dowaload kaggle data in zip format

unzip data

!unzip titanic.zip -d titanicDataset

here I have unzipped data in titanicDataset folder

go to titanicDatset folder

%cd titanicDataset

now you can read titanic data

import pandas as pd
df = pd.read_csv("train.csv")
df.head()

Using this method you can download any data i.e. image, video, audio..

But remember there is limit in google drive, in my case it is 15 GB so you can download large data

watch this tutorial on YouTube here

If you have used this tutorial to learn something than clap for this tutorial and give response, if you are getting any problem let me know.

Join our Telegram channel for more updates, study resources and discussion

👉 https://t.me/joinai

--

--