Import data into Google Colaboratory
Solution 1:
An official example notebook demonstrating local file upload/download and integration with Drive and sheets is available here: https://colab.research.google.com/notebooks/io.ipynb
The simplest way to share files is to mount your Google Drive.
To do this, run the following in a code cell:
from google.colab import drive
drive.mount('/content/drive')
It will ask you to visit a link to ALLOW "Google Files Stream" to access your drive. After that a long alphanumeric auth code will be shown that needs to be entered in your Colab's notebook.
Afterward, your Drive files will be mounted and you can browse them with the file browser in the side panel.
Here's a full example notebook
Solution 2:
Upload
from google.colab import files
files.upload()
Download
files.download('filename')
List directory
files.os.listdir()
Solution 3:
step 1- Mount your Google Drive to Collaboratory
from google.colab import drive
drive.mount('/content/gdrive')
step 2- Now you will see your Google Drive files in the left pane (file explorer). Right click on the file that you need to import and select çopy path. Then import as usual in pandas, using this copied path.
import pandas as pd
df=pd.read_csv('gdrive/My Drive/data.csv')
Done!
Solution 4:
Simple way to import data from your googledrive - doing this save people time (don't know why google just doesn't list this step by step explicitly).
INSTALL AND AUTHENTICATE PYDRIVE
!pip install -U -q PyDrive ## you will have install for every colab session
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
UPLOADING
if you need to upload data from local drive:
from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(name=fn, length=len(uploaded[fn])))
execute and this will display a choose file button - find your upload file - click open
After uploading, it will display:
sample_file.json(text/plain) - 11733 bytes, last modified: x/xx/2018 - %100 done
User uploaded file "sample_file.json" with length 11733 bytes
CREATE FILE FOR NOTEBOOK
If your data file is already in your gdrive, you can skip to this step.
Now it is in your google drive. Find the file in your google drive and right click. Click get 'shareable link.' You will get a window with:
https://drive.google.com/open?id=29PGh8XCts3mlMP6zRphvnIcbv27boawn
Copy - '29PGh8XCts3mlMP6zRphvnIcbv27boawn' - that is the file ID.
In your notebook:
json_import = drive.CreateFile({'id':'29PGh8XCts3mlMP6zRphvnIcbv27boawn'})
json_import.GetContentFile('sample.json') - 'sample.json' is the file name that will be accessible in the notebook.
IMPORT DATA INTO NOTEBOOK
To import the data you uploaded into the notebook (a json file in this example - how you load will depend on file/data type - .txt,.csv etc. ):
sample_uploaded_data = json.load(open('sample.json'))
Now you can print to see the data is there:
print(sample_uploaded_data)
Solution 5:
The simplest way I've made is :
- Make repository on github with your dataset
- Clone Your repository with ! git clone --recursive [GITHUB LINK REPO]
- Find where is your data ( !ls command )
- Open file with pandas as You do it in normal jupyter notebook.