Import Kaggle Datasets into Google Colab


Introduction

Google Colab offers a powerful Python environment with free access to GPUs, ideal for machine learning and data analysis tasks. If you're working with datasets from Kaggle, you can easily connect the two platforms using the Kaggle API.

In this guide, we'll show you how to import Kaggle datasets into Google Colab in five simple steps.

Sign in to Kaggle account

Step 1: Get Your Kaggle API Key

  1. Log in to your Kaggle account at kaggle.com
  2. Scroll to the API section inside Settings tab ( click your profile picture at right )
  3. Click on "Create New API Token"
  4. This downloads a file named kaggle.json to your computer
Settings link inside Kaggle account

Download the kaggle.json file to your local system.
Creating new token to download kaggle.json file

Step 2: Upload kaggle.json to Colab

Use the below code in your Colab notebook to upload the kaggle.json file.

from google.colab import files
files.upload()  # Choose kaggle.json when prompted

Step 3: Configure API Access

Move the file to the correct location and set permissions:

# Create the directory if it doesn't exist
!mkdir -p ~/.kaggle

# Move kaggle.json to the correct directory. Assumes kaggle.json is in the current working directory.
# If your kaggle.json is in a different location, please update the path below.
!mv kaggle.json ~/.kaggle/

# Set permissions for the kaggle.json file
!chmod 600 ~/.kaggle/kaggle.json

Step 4: Download a Dataset

Creating new token to download kaggle.json file

Go to the Kaggle dataset page and copy the dataset name from the URL. For example, for:

kagglehub.dataset_download("yasserh/titanic-dataset")

The dataset path is: yasserh/titanic-dataset

Use the command below to download:

!kaggle datasets download -d yasserh/titanic-dataset

Step 5: Unzip and Load the Dataset

# Unzip the downloaded dataset
!unzip -q titanic-dataset.zip
Remove other files
# Remove the zip file after extraction
!rm titanic-dataset.zip

# Remove other metadata files if they exist and are not needed
!rm -f titanic-dataset.zip.json
Load Dataset by using Pandas
import pandas as pd
df = pd.read_csv("Titanic-Dataset.csv")
df.head()
How many rows and columns are there in Dataset ?
# Get the total number of rows and columns
num_rows, num_cols = df.shape

print(f"Total number of rows: {num_rows}")
print(f"Total number of columns: {num_cols}")
📦 Want to Add Interactivity to Your Colab?

Enhance your notebook using IPyWidgets for sliders, buttons, and more.

Explore IPyWidgets →



Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer