0% found this document useful (0 votes)
14 views

Loading Data in Colab

This document discusses two options for uploading datasets to a Colab notebook from a local filesystem or Google Drive. The local upload option uses the files.upload function to add a "Choose Files" button and upload files as an encoded dictionary. Pandas can then be used to load CSV and Excel files from the uploaded data into DataFrames. For Google Drive, the drive.mount command maps the user's drive where data can be read directly into DataFrames using file paths.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Loading Data in Colab

This document discusses two options for uploading datasets to a Colab notebook from a local filesystem or Google Drive. The local upload option uses the files.upload function to add a "Choose Files" button and upload files as an encoded dictionary. Pandas can then be used to load CSV and Excel files from the uploaded data into DataFrames. For Google Drive, the drive.mount command maps the user's drive where data can be read directly into DataFrames using file paths.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Uploading dataset from local filesystem

This option will create a “Choose File” button in your notebook, using which you can upload

your dataset to the notebook’s runtime.

from google.colab import files


uploaded = files.upload()

This will add a “Choose Files” button and you can upload your dataset.  files.upload returns a
dictionary of the files which were uploaded. The dictionary is keyed by the file name, the
value is the data which was uploaded. Note that the uploaded file object is encoded so we
have to decode it before use.
Using pandas to store the uploaded csv file into a DataFrame.
import pandas as pd
import io
train_data = pd.read_csv(io.StringIO(uploaded['train.csv'].decode('utf-8')))

Using pandas to store the uploaded excel file into a DataFrame.

import pandas as pd
import io
data = pd.read_excel(io.BytesIO(uploaded['Online_Retail.xlsx']))

Uploading dataset from GoogleDrive

The following code will mount the drive.

from google.colab import drive

drive.mount('/content/gdrive')

It will ask for a authorisation code through a link. after signing into your Google account get the

code and paste in colab

test_data = pd.read_csv('/content/gdrive/My Drive/Taxi_Trips.csv')


Adding dataset that is uploaded in drive to the Colab Notebook.

test_data = pd.read_csv('/content/gdrive/My Drive/Taxi_Trips.csv')

You might also like