Artificial Intelligence

How to Use Kaggle Datasets in Google Colab

Download datasets directly to colab using kaggle API.

Soner Yıldırım
June 4, 20202 min read
Photo by Nick Fewings on Unsplash
Photo by Nick Fewings on Unsplash

Kaggle is a very popular platform among people in data science domain. Its fame comes from the competitions but there are also many datasets that we can work on for practice. In this post, we will see how to import datasets from Kaggle directly to google colab notebooks.

We first go to our account page on Kaggle to generate an API token. On the account page, we scroll down to API section.

If there is a previously generated API token, we should expire it by clicking on "Expire API Token". If not, we can directly generate a token clicking on "Create New API Token". When clicked on, a file named kaggle.json will be download.

Then we open a new notebook in colab and run the following command.

text
!pip install -q kaggle

Then we need to upload kaggle.json file.

text
from google.colab import files
text
files.upload()

Choose "kaggle.json" downloaded from Kaggle. We need save this file into a directory named kaggle. Run the following commands to accomplish this task:

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

We now change the permission of the file using the following command:

text
!chmod 600 ~/.kaggle/kaggle.json

We can now see the datasets available on kaggle:

text
!kaggle datasets list

To download a particular one:

text
!kaggle datasets download -d "name_of_the_dataset"

For instance:

text
!kaggle datasets download -d ruchi798/tv-shows-on-netflix-prime-video-hulu-and-disney

We now have the data available in colab:

Let's read it into a pandas dataframe:

text
import numpy as npimport pandas as pd
text
df = pd.read_csv("/content/tv-shows-on-netflix-prime-video-hulu-and-disney.zip")
text
df.head()

We don't have to go through all of these steps everytime. Once these steps are completed, next time we just upload kaggle.json to colab and then download the dataset.

text
!kaggle datasets download -d kianwee/agricultural-raw-material-prices-19902020

We can always check the available datasets with !kaggle datasets list


I consider kaggle and google colab as two key players in the field of data science. Both contribute to the community and thus lead to advancements in data science. It is nice to create a connection between the two.

Thank you for reading. Please let me know if you have any feedback.

Related Articles