How to use Kaggle datasets in Google Colab?
Published in
3 min readJun 23, 2021
A quick guide to use Kaggle datasets inside Google Colab
- Download the Kaggle API token.
- Sign in to the Kaggle Account. If you don’t have a Kaggle account, first sign up.
- Go to “Account”, go down the page, and find the “API” section.
- Click the “Create New API Token” button.
- The “kaggle.json” file will be downloaded.
- You’ll be also able to see a message “Ensure kaggle.json is in the location ~/.kaggle/kaggle.json to use the API” in a green background.
- If you open the “kaggle.json” file, you can see your Kaggle “username” and a “key”.
2. Mount the Google drive to the Colab notebook.
What does “Mount” Google Drive mean?
It means giving access to the files in your google drive to Colab notebook.
- Go to your Colab notebook. Run the following lines of codes.
from google.colab import drivedrive.mount("/content/gdrive", force_remount=True)
- Now you can access the files in Google Drive from the Colab notebook.
- Later we’re going to download the Kaggle dataset into your google drive. So you can access that dataset from the Colab notebook.
3 . Upload the “kaggle.json” file into Google drive.
- Go to the folder in google drive where you want to download the Kaggle dataset.
- Upload the “kaggle.json” into that folder.
4. Install Kaggle API.
!pip install kaggle
- Change the current working directory to “/content/gdrive/MyDrive/DataSets/house_price_data/”
%cd /content/gdrive/MyDrive/DataSets/house_price_data/
- Run the following code to configure the path to “kaggle.json”.
import osos.environ['KAGGLE_CONFIG_DIR'] = "/content/gdrive/MyDrive/DataSets/house_price_data/"
5. Download the dataset.
- Here I’m going to download the “House Prices dataset”.
- You may need to click the “I understand and accept” button under the “Rules ” section to agree to be bound with the rules.
- Click the “Data” section and copy the following line of code.
- Paste the code in the Colab notebook.
- Add the “!” mark at the front and run the code.
!kaggle competitions download -c house-prices-advanced-regression-techniques
- Downloading …
- Run the “ls” command to check the dataset files.
- Dataset was successfully downloaded. Now load the dataset and have fun!
References: