Download Kaggle Datasets into Google Colab

Kaggle provides datasets for data analysis, machine learning and Python projects. We can download these datasets directly into Google Colab, extract the files and load them into a Pandas DataFrame without first downloading the dataset manually to our computer.

This tutorial covers two Kaggle authentication methods. The recommended approach uses a Kaggle API token stored securely in Google Colab Secrets. The older kaggle.json method is also included because it is still supported and is used by many existing notebooks.


Import and Use Kaggle Datasets in Google Colab

Install or Update the Kaggle CLI 🔝

Start a Google Colab notebook and install the current Kaggle command-line package.

!pip install -q --upgrade kaggle

Check the installed version:

!kaggle --version

The kaggle command can now be used from Colab cells.

Method 1: Kaggle API Token with Google Colab Secrets 🔝

This is the preferred approach for a new notebook because the credential does not have to be written directly into the notebook code.

Step 1: Generate a Kaggle API Token

  1. Sign in to your Kaggle account.
  2. Open your account Settings.
  3. Go to the API section.
  4. Generate a new API token.
  5. Copy the generated token.
Sign in to Kaggle account

Kaggle account Settings page

Step 2: Add the Token to Colab Secrets

In Google Colab, open the Secrets section from the left sidebar and add a secret named:

KAGGLE_API_TOKEN

Paste the Kaggle token as its value and allow the notebook to access the secret.

Step 3: Load the Secret

from google.colab import userdata
import os

os.environ['KAGGLE_API_TOKEN']=userdata.get('KAGGLE_API_TOKEN')

Test Kaggle Access

!kaggle datasets list -s titanic

If authentication is working, Kaggle returns matching datasets.

Method 2: Upload the Legacy kaggle.json File 🔝

The older Kaggle API authentication method uses a file named kaggle.json. This method is still useful for older notebooks and tutorials.

Create the Legacy API Key

From Kaggle Settings, locate the legacy API credentials option and create the legacy API key. This downloads:

kaggle.json
Creating Kaggle API credentials

Upload kaggle.json to Colab

from google.colab import files

files.upload() # Select kaggle.json

Move the File to the Kaggle Configuration Directory

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

The chmod command limits access to the credential file.

Test the Configuration

!kaggle datasets list -s titanic

Find the Kaggle Dataset Handle 🔝

Kaggle datasets use a handle in this format:

owner/dataset-name

For example, the dataset URL may contain:

yasserh/titanic-dataset

This is the value used by the Kaggle CLI.

Finding the Kaggle dataset download identifier

Search for a Dataset from Colab

!kaggle datasets list -s titanic

List Files before Downloading

!kaggle datasets files yasserh/titanic-dataset

This is useful when a dataset contains several files and you want to inspect them before downloading.

Download a Kaggle Dataset 🔝

Download as a ZIP File

!kaggle datasets download -d yasserh/titanic-dataset

The dataset is downloaded to the current Colab working directory.

Download and Extract in One Step

!kaggle datasets download -d yasserh/titanic-dataset --unzip

The --unzip option extracts the downloaded archive and removes the ZIP file after successful extraction.

Download to a Separate Folder

!mkdir -p data
!kaggle datasets download -d yasserh/titanic-dataset -p data --unzip

Keeping dataset files inside a separate directory makes larger notebooks easier to manage.

Check the Downloaded Files 🔝

For files downloaded to the current directory:

!ls -lh

If a data directory was used:

!ls -lh data

Check Files with Python

from pathlib import Path

for file in Path('data').iterdir():
    print(file.name)

Load the Kaggle Dataset with Pandas 🔝

import pandas as pd

df=pd.read_csv('data/Titanic-Dataset.csv')
df.head()

Number of Rows and Columns

rows, columns=df.shape

print(f'Rows: {rows}')
print(f'Columns: {columns}')

Column Names

print(df.columns.tolist())

Dataset Information

df.info()
Colab runtime storage is temporary. Files downloaded into the runtime can disappear when the session is reset or disconnected. Save important output to Google Drive or another persistent location when required.

Run the Complete Plus2net Notebook 🔝

Practice this tutorial directly in Google Colab or view the notebook source on GitHub.

Open in Google Colab View on GitHub

The notebook can also be downloaded from GitHub as an .ipynb file.

Alternative: Download with kagglehub 🔝

Kaggle also provides the kagglehub Python library. This is separate from the kaggle command-line examples above.

Install kagglehub

!pip install -q --upgrade kagglehub

Download a Dataset

import kagglehub

path=kagglehub.dataset_download('yasserh/titanic-dataset')
print(path)

dataset_download() returns the local directory containing the downloaded dataset.

Convert the Downloaded CSV to Other Formats 🔝

After loading the Kaggle CSV into a Pandas DataFrame, the same data can be saved in other formats such as SQLite, JSON and XML.

Convert CSV Data to SQLite 🔝

Use Pandas to_sql() to write the DataFrame to an SQLite table.

import pandas as pd
import sqlite3

df=pd.read_csv('data/Titanic-Dataset.csv')

with sqlite3.connect('titanic.db') as conn:
    df.to_sql('titanic_data', conn, if_exists='replace', index=False)

Check the SQLite Data

with sqlite3.connect('titanic.db') as conn:
    db_df=pd.read_sql_query('SELECT * FROM titanic_data LIMIT 5', conn)

db_df

Convert CSV Data to JSON 🔝

More on JSON Format

import pandas as pd

df=pd.read_csv('data/Titanic-Dataset.csv')
df.to_json('titanic.json', orient='records', indent=4)

Check the JSON File

json_df=pd.read_json('titanic.json')
json_df.head()

Convert CSV Data to XML 🔝

More on XML Format

Pandas can write the DataFrame directly as XML.

import pandas as pd

df=pd.read_csv('data/Titanic-Dataset.csv')
df.to_xml('titanic.xml', root_name='TitanicData', row_name='Record', index=False)

Read the XML Back into Pandas

xml_df=pd.read_xml('titanic.xml')
xml_df.head()

See the Pandas read_xml() tutorial for more XML parsing examples.

Common Kaggle and Colab Errors 🔝

kaggle: command not found

Install or update the package:

!pip install -q --upgrade kaggle

401 Unauthorized

The credential may be missing, invalid or expired. Generate a valid Kaggle token and configure it again.

Could not find kaggle.json

This occurs when using legacy authentication and the file is not available in the expected location.

!ls -l ~/.kaggle/kaggle.json

Dataset Not Found

Check that the dataset handle uses:

owner/dataset-name

and verify the available files:

!kaggle datasets files owner/dataset-name

Permission or Access Error

Some Kaggle resources require account access, license acceptance or competition-rule acceptance before they can be downloaded through the API.

File Missing after Restarting Colab

Colab runtime storage is temporary. Re-run the download cells or save required output in persistent storage.

Kaggle Dataset in Google Colab: Summary 🔝

  • Install or update the kaggle package before using the CLI.
  • The current recommended authentication uses a Kaggle API token.
  • Google Colab Secrets can keep the token outside visible notebook code.
  • The older kaggle.json authentication method is still useful for legacy notebooks.
  • Never publish Kaggle credentials in GitHub or a public Colab notebook.
  • A Kaggle dataset handle uses the format owner/dataset-name.
  • kaggle datasets list can search for datasets.
  • kaggle datasets files lists files inside a dataset.
  • kaggle datasets download downloads dataset files.
  • --unzip can download and extract a dataset in one step.
  • Pandas read_csv() loads CSV data into a DataFrame.
  • df.shape gives the number of rows and columns.
  • kagglehub.dataset_download() provides an alternative Python-based download workflow.
  • Downloaded Colab runtime files are temporary unless saved to persistent storage.
  • Pandas can convert the downloaded data to SQLite, JSON and XML.
Continue learning: Return to the Google Colab tutorial for notebook basics, or use IPyWidgets when you want interactive controls such as buttons, sliders and dropdowns inside a notebook.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer