How to Use the Kaggle API in Python
Datasets

Kaggle gives us several options for downloading datasets. The two you’re most likely to use are for downloading competition datasets, or standalone datasets.

A competition dataset is related to a current or past competition, for example, the dataset used in the Sentiment Analysis on Movie Reviews competition.

Standalone datasets are not accompanied by a competition and can be uploaded by anyone — like this 1.6M Sentiment of Tweets dataset.

We use two different methods for each of these.

Competition Datasets

We can see that our dataset is paired with a competition through the URL of the dataset, it will always begin with kaggle.com/c/ — the c representing competition.

To download a competition dataset, we use the competition_download_file method, take the competition name (given in the URL) and write:

Here we download both the training and test datasets to the current directory ./ — both are zipped.

Alternatively, we can simply download all competition datasets with:

api.competition_download_files('sentiment-analysis-on-movie-reviews', path='./')

You may need to setup your local directory to receive them without error — I always find downloading each individual dataset more convenient.

Standalone Datasets

On the dataset page, we can see the user’s name and the dataset name (or in the address bar). We put both together like user/dataset , and execute dataset_download_file like so:

This will download the zipped file into our current directory ./ .

Again, just like we did with the competition datasets, we can download all files for a specific dataset like so:

api.dataset_download_files('kazanova/sentiment140', path='./')

Unzipping

A final point, every dataset you download with the Kaggle API will be downloaded as a ZIP file. You can unzip the data manually, or simply use Python like so:

Once unzipped, we read our data into Python as per usual!
['James Briggs']
['Python', 'Technology', 'Data Science', 'Programming', 'Machine Learning']