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.
kaggle.json to GitHub, or share either credential with other users.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.
This is the preferred approach for a new notebook because the credential does not have to be written directly into the notebook code.


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.
from google.colab import userdata
import os
os.environ['KAGGLE_API_TOKEN']=userdata.get('KAGGLE_API_TOKEN')
!kaggle datasets list -s titanic
If authentication is working, Kaggle returns matching datasets.
The older Kaggle API authentication method uses a file named kaggle.json. This method is still useful for older notebooks and tutorials.
From Kaggle Settings, locate the legacy API credentials option and create the legacy API key. This downloads:
kaggle.json

from google.colab import files
files.upload() # Select kaggle.json
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/kaggle.json
!chmod 600 ~/.kaggle/kaggle.json
The chmod command limits access to the credential file.
!kaggle datasets list -s titanic
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.

!kaggle datasets list -s titanic
!kaggle datasets files yasserh/titanic-dataset
This is useful when a dataset contains several files and you want to inspect them before downloading.
!kaggle datasets download -d yasserh/titanic-dataset
The dataset is downloaded to the current Colab working directory.
!kaggle datasets download -d yasserh/titanic-dataset --unzip
The --unzip option extracts the downloaded archive and removes the ZIP file after successful extraction.
!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.
For files downloaded to the current directory:
!ls -lh
If a data directory was used:
!ls -lh data
from pathlib import Path
for file in Path('data').iterdir():
print(file.name)
import pandas as pd
df=pd.read_csv('data/Titanic-Dataset.csv')
df.head()
rows, columns=df.shape
print(f'Rows: {rows}')
print(f'Columns: {columns}')
print(df.columns.tolist())
df.info()
Practice this tutorial directly in Google Colab or view the notebook source on GitHub.
Open in Google Colab View on GitHubThe notebook can also be downloaded from GitHub as an .ipynb file.
Kaggle also provides the kagglehub Python library. This is separate from the kaggle command-line examples above.
!pip install -q --upgrade kagglehub
import kagglehub
path=kagglehub.dataset_download('yasserh/titanic-dataset')
print(path)
dataset_download() returns the local directory containing the downloaded dataset.
kagglehub when a Python function fits your notebook workflow better.After loading the Kaggle CSV into a Pandas DataFrame, the same data can be saved in other formats such as SQLite, JSON and XML.
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)
with sqlite3.connect('titanic.db') as conn:
db_df=pd.read_sql_query('SELECT * FROM titanic_data LIMIT 5', conn)
db_df
import pandas as pd
df=pd.read_csv('data/Titanic-Dataset.csv')
df.to_json('titanic.json', orient='records', indent=4)
json_df=pd.read_json('titanic.json')
json_df.head()
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)
xml_df=pd.read_xml('titanic.xml')
xml_df.head()
See the Pandas read_xml() tutorial for more XML parsing examples.
Install or update the package:
!pip install -q --upgrade kaggle
The credential may be missing, invalid or expired. Generate a valid Kaggle token and configure it again.
This occurs when using legacy authentication and the file is not available in the expected location.
!ls -l ~/.kaggle/kaggle.json
Check that the dataset handle uses:
owner/dataset-name
and verify the available files:
!kaggle datasets files owner/dataset-name
Some Kaggle resources require account access, license acceptance or competition-rule acceptance before they can be downloaded through the API.
Colab runtime storage is temporary. Re-run the download cells or save required output in persistent storage.
kaggle package before using the CLI.kaggle.json authentication method is still useful for legacy notebooks.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.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.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.