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')