Upload data to google colab

Upload data to google colab DEFAULT

How to Deal with Files in Google Colab: Everything You Need to Know

Google Colaboratory is a free Jupyter notebook environment that runs on Google’s cloud servers, letting the user leverage backend hardware like GPUs and TPUs. This lets you do everything you can in a Jupyter notebook hosted in your local machine, without requiring the installations and setup for hosting a notebook in your local machine.

Colab comes with (almost) all the setup you need to start coding, but what it doesn’t have out of the box is your datasets! How do you access your data from within Colab?

In this article we will talk about:

  • How to load data to Colab from a multitude of data sources
  • How to write back to those data sources from within Colab
  • Limitations of Google Colab while working with external files

Directory and file operations in Google Colab

Since Colab lets you do everything which you can in a locally hosted Jupyter notebook, you can also use shell commands like , et cetera using line-magic (%) or bash (!). 

To browse the directory structure, you can use the file-explorer pane on the left.

google colab directory

How to upload files to and download files from Google Colab

Since a Colab notebook is hosted on Google’s cloud servers, there’s no direct access to files on your local drive (unlike a notebook hosted on your machine) or any other environment by default. 

However, Colab provides various options to connect to almost any data source you can imagine. Let us see how.

Accessing GitHub from Google Colab

You can either clone an entire GitHub repository to your Colab environment or access individual files from their raw link.

Clone a GitHub repository

You can clone a GitHub repository into your Colab environment in the same way as you would in your local machine, using git . Once the repository is cloned, refresh the file-explorer to browse through its contents. 

Then you can simply read the files as you would in your local machine.

colab github repository

Load individual files directly from GitHub

In case you just have to work with a few files rather than the entire repository, you can load them directly from GitHub without needing to clone the repository to Colab.

To do this:

  1. click on the file in the repository, 
  2. click on View Raw,
  3. copy the URL of the raw file, 
  4. use this URL as the location of your file. 

Accessing Local File System to Google Colab

You can read from or write to your local file system either using the file-explorer, or Python code:

Access local files through the file-explorer

Uploading files from local file system through file-explorer

You can either use the upload option at the top of the file-explorer pane to upload any file(s) from your local file system to Colab in the present working directory. 

To upload files directly to a subdirectory you need to:

1. Click on the three dots visible when you hover above the directory 

2. Select the “upload” option.

colab upload

3. Select the file(s) you wish to upload from the “File Upload” dialog window.

4. Wait for the upload to complete. The upload progress is shown at the bottom of the file-explorer pane.

colab upload progress

Once the upload is complete, you can read from the file as you would normally.

colab upload complete

Downloading files to local file system through file-explorer

Click on the three dots which are visible while hovering above the filename, and select the “download” option.

colab download

Accessing local file system using Python code

This step requires you to first import the module from the :

from google.colab import files

Uploading files from local file system using Python code

You use the upload method of the object:

uploaded = files.upload()

Running this opens the File Upload dialog window:

colab file upload

Select the file(s) you wish to upload, and then wait for the upload to complete. The upload progress is displayed:

colab file upload progress

The object is a dictionary having the filename and content as it’s key-value pairs:

colab file uploaded

Once the upload is complete, you can either read it as any other file from colab:

df4 = pd.read_json("News_Category_Dataset_v2.json", lines=True)

Or read it directly from the dict using the library: 

import io df5 = pd.read_json(io.BytesIO(uploaded['News_Category_Dataset_v2.json']), lines=True)

Make sure that the filename matches the name of the file you wish to load.

Downloading files from Colab to local file system using Python code:

The method of the files object can be used to download any file from colab to your local drive. The download progress is displayed, and once the download completes, you can choose where to save it in your local machine.

colab downloading

Accessing Google Drive from Google Colab

You can use the module from to mount your entire Google Drive to Colab by:

1. Executing the below code which will provide you with an authentication link

from google.colab import drive drive.mount('/content/gdrive')

2. Open the link

3. Choose the Google account whose Drive you want to mount

4. Allow Google Drive Stream access to your Google Account

5. Copy the code displayed, paste it in the text box as shown below, and press Enter

colab import drive

Once the Drive is mounted, you’ll get the message , and you’ll be able to browse through the contents of your Drive from the file-explorer pane.

colab drive

Now you can interact with your Google Drive as if it was a folder in your Colab environment. Any changes to this folder will reflect directly in your Google Drive. You can read the files in your Google Drive as any other file.

You can even write directly to Google Drive from Colab using the usual file/directory operations.

!touch "/content/gdrive/My Drive/sample_file.txt"

This will create a file in your Google Drive, and will be visible in the file-explorer pane once you refresh it:

colab drive files
colab my drive

Accessing Google Sheets from Google Colab

To access Google Sheets:

1. You need to first authenticate the Google account to be linked with Colab by running the code below:

from google.colab import auth auth.authenticate_user()

2. Executing the above code will provide you with an authentication link. Open the link, 

3. Choose the Google account which you want to link, 

4. Allow Google Cloud SDK to access your Google Account, 

5. Finally copy the code displayed and paste it in the text box shown, and hit Enter.

colab code

To interact with Google Sheets, you need to import the preinstalled library. And to authorize access to your Google account, you need the method from the preinstalled library:

import gspread from oauth2client.client import GoogleCredentials gc = gspread.authorize(GoogleCredentials.get_application_default())

Once the above code is run, an Application Default Credentials (ADC) JSON file will be created in the present working directory. This contains the credentials used by to access your Google account. 

colab adc json

Once this is done, you can now create or load Google sheets directly from your Colab environment.

Creating/updating a Google Sheet in Colab

1. Use the object’s create method to a workbook:

wb = gc.create('demo')

2. Once the workbook is created, you can view it in sheets.google.com.

colab google sheets

3. To write values to the workbook, first open a worksheet:

ws = gc.open('demo').sheet1

4. Then select the cell(s) you want to write to:

colab cells

5. This creates a list of cells with their index (R1C1) and value (currently blank). You can modify the individual cells by updating their value attribute:

colab cells values

6. To update these cells in the worksheet, use the update_cells method:

colab cells values updated

7. The changes will now be reflected in your Google Sheet.

colab sheet

Downloading data from a Google Sheet

1. Use the object’s method to open a workbook:

wb = gc.open('demo')

2. Then read all the rows of a specific worksheet by using the method:

colab rows

3. To load these to a dataframe, you can use the DataFrame object’s method:

colab dataframe

Accessing Google Cloud Storage (GCS) from Google Colab

You need to have a Google Cloud Project (GCP) to use GCS. You can create and access your GCS buckets in Colab via the preinstalled command-line utility.

1. First specify your project ID:

project_id = '<project_ID>'

2. To access GCS, you’ve to authenticate your Google account:

from google.colab import auth auth.authenticate_user()

3. Executing the above code will provide you with an authentication link. Open the link, 

4. Choose the Google account which you want to link, 

5. Allow Google Cloud SDK to access your Google Account, 

6. Finally copy the code displayed and paste it in the text box shown, and hit Enter.

colab code

7. Then you configure to use your project:

!gcloud config set project {project_id}

8. You can make a bucket using the make bucket () command. GCP buckets must have a universally unique name, so use the preinstalled library to generate a Universally Unique ID:

import uuid bucket_name = f'sample-bucket-{uuid.uuid1()}' !gsutil mb gs://{bucket_name}

9. Once the bucket is created, you can upload a file from your colab environment to it:

!gsutil cp /tmp/to_upload.txt gs://{bucket_name}/

10. Once the upload has finished, the file will be visible in the GCS browser for your project: https://console.cloud.google.com/storage/browser?project=<project_id>

!gsutil cp gs://{bucket_name}/{filename} {download_location}

Once the download has finished, the file will be visible in the Colab file-explorer pane in the download location specified.

Accessing AWS S3 from Google Colab

You need to have an AWS account, configure IAM, and generate your access key and secret access key to be able to access S3 from Colab. You also need to install the library to your colab environment:

1. Install the library

!pip install awscli

2. Once installed, configure AWS by running :

colab access
  1. Enter your and in the text boxes, and press enter.

Then you can download any file from S3:

!aws s3 cp s3://{bucket_name} ./{download_location} --recursive --exclude "*" --include {filepath_on_s3}

can point to a single file, or match multiple files using a pattern.

You will be notified once the download is complete, and the downloaded file(s) will be available in the location you specified to be used as you wish. 

To upload a file, just reverse the source and destination arguments:

!aws s3 cp ./{upload_from} s3://{bucket_name} --recursive --exclude "*" --include {file_to_upload}

can point to a single file, or match multiple files using a pattern.

You will be notified once the upload is complete, and the uploaded file(s) will be available in your S3 bucket in the folder specified: https://s3.console.aws.amazon.com/s3/buckets/{bucket_name}/{folder}/?region={region}

Accessing Kaggle datasets from Google Colab

To download datasets from Kaggle, you first need a Kaggle account and an API token. 

1. To generate your API token, go to “My Account”, then “Create New API Token”. 

2. Open the kaggle.json file, and copy its contents. It should be in the form of {}.

3. Then run the below commands in Colab:

!mkdir ~/.kaggle !echo '<PASTE_CONTENTS_OF_KAGGLE_API_JSON>' > ~/.kaggle/kaggle.json !chmod 600 ~/.kaggle/kaggle.json !pip install kaggle

4. Once the kaggle.json file has been created in Colab, and the Kaggle library has been installed, you can search for a dataset using

!kaggle datasets list -s {KEYWORD}

5. And then download the dataset using

!kaggle datasets download -d {DATASET NAME} -p /content/kaggle/

The dataset will be downloaded and will be available in the path specified ( in this case).

Accessing MySQL databases from Google Colab

1. You need to import the preinstalled library to work with relational databases:

import sqlalchemy

2. Enter the connection details and create the engine:

HOSTNAME = 'ENTER_HOSTNAME' USER = 'ENTER_USERNAME' PASSWORD = 'ENTER_PASSWORD' DATABASE = 'ENTER_DATABASE_NAME' connection_string = f'mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOSTNAME}/{MYSQL_DATABASE}' engine = sqlalchemy.create_engine(connection_string)

3. Finally, just create the SQL query, and load the query results to a dataframe using

query = f"SELECT * FROM {DATABASE}.{TABLE}"import pandas as pd df = pd.read_sql_query(query, engine)

Limitations of Google Colab while working with Files

One important caveat to remember while using Colab is that the files you upload to it won’t be available forever. Colab is a temporary environment with an idle timeout of 90 minutes and an absolute timeout of 12 hours. This means that the runtime will disconnect if it has remained idle for 90 minutes, or if it has been in use for 12 hours. On disconnection, you lose all your variables, states, installed packages, and files and will be connected to an entirely new and clean environment on reconnecting.

Also, Colab has a disk space limitation of 108 GB, of which only 77 GB is available to the user. While this should be enough for most tasks, keep this in mind while working with larger datasets like image or video data.


Google Colab is a great tool for individuals who want to harness the power of high-end computing resources like GPUs, without being restricted by their price. 

In this article, we have gone through most of the ways you can supercharge your Google Colab experience by reading external files or data in Google Colab and writing from Google Colab to those external data sources. 

Depending on your use-case, or how your data architecture is set-up, you can easily apply the above-mentioned methods to connect your data source directly to Colab, and start coding!

Other resources 

Siddhant Sadangi

Currently working as a Data Scientist with Reuters helping their Editorial, Marketing, and Sales teams derive insights from data, he strongly believes that the best way to learn is to teach. Knowledge multiplies when shared 🙂


How to Use Google Colab for Deep Learning – Complete Tutorial

9 mins read | Author Harshit Dwivedi | Updated June 8th, 2021

If you’re a programmer, you want to explore deep learning, and need a platform to help you do it – this tutorial is exactly for you.

Google Colab is a great platform for deep learning enthusiasts, and it can also be used to test basic machine learning models, gain experience, and develop an intuition about deep learning aspects such as hyperparameter tuning, preprocessing data, model complexity, overfitting and more.

Let’s explore!


Colaboratory by Google (Google Colab in short) is a Jupyter notebook based runtime environment which allows you to run code entirely on the cloud.

This is necessary because it means that you can train large scale ML and DL models even if you don’t have access to a powerful machine or a high speed internet access.

Google Colab supports both GPU and TPU instances, which makes it a perfect tool for deep learning and data analytics enthusiasts because of computational limitations on local machines. 

Since a Colab notebook can be accessed remotely from any machine through a browser, it’s well suited for commercial purposes as well.

In this tutorial you will learn:

  • Getting around in Google Colab
  • Installing python libraries in Colab
  • Downloading large datasets in Colab 
  • Training a Deep learning model in Colab
  • Using TensorBoard in Colab
Continue reading ->
Sours: https://neptune.ai/blog/google-colab-dealing-with-files

Ways to import CSV files in Google Colab

Colab (short for Colaboratory) is Google’s free platform which enables users to code in Python. It is a Jupyter Notebook-based cloud service, provided by Google. This platform allows us to train the Machine Learning models directly in the cloud and all for free. Google Colab does whatever your Jupyter Notebook does and a bit more, i.e. you can use GPU and TPU for free. Some of Google Colab’s advantages include quick installation and real-time sharing of Notebooks between users. 

However, when loading a CSV file it requires to write some extra line of codes. In this article, we will be discussing three different ways to load a CSV file and store it in a pandas dataframe. To get started, sign in to your Google Account, and then go to “https://colab.research.google.com” and click on “New Notebook”

Ways to import CSV

Load data from local drive 

To upload the file from the local drive write the following code in the cell and run it




you will get a screen as, 

Click on “choose files”, then select and download the CSV file from your local drive.  Later write the following code snippet to import it into a pandas dataframe.




From Github 

It is the easiest way to to upload a CSV file in Colab. For this go to the dataset in your github repository, and then click on “View Raw”. Copy the link to the raw dataset and pass it as a parameter to the read_csv() in pandas to get the dataframe. 



From your Google drive

We can import datasets that are uploaded on our google drive in two ways : 

1. Using PyDrive 
This is the most complex method for importing datasets among all. For this we first require to install PyDrive library from python installer(pip) and execute the following.






Click on the link prompted to get the authentication to allow Google to access your Drive. You will see a screen with “Google Cloud SDK wants to access your Google Account” at the top. After you allow permission, copy the given verification code and paste it in the box in Colab. 

Now, go to the CSV file in your Drive and get the shareable link and store it in a string variable in Colab. Now, to get this file in dataframe run the following code.


2. Mounting the drive 
This method is quite simple and clean than the above mentioned method. 

  • Create a folder in your Google Drive. 
  • Upload the CSV file in this folder. 
  • Write the following code in your Colab Notebook : 
from google.colab import drive drive.mount(‘/content/drive’)

Just like with the previous method, the commands will bring you to a Google Authentication step. Later complete the verification as we did in the last method. Now in the Notebook, at the top-left there is File menu and then click on Locate in Drive, and then find your data. Then copy the path of the CSV file in a variable in your notebook, and read the file using read_csv(). 

path = "copied path" df_bonus = pd.read_csv(path)

Now, to read the file run the following code.




 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

Sours: https://www.geeksforgeeks.org/ways-to-import-csv-files-in-google-colab/
  1. Isle of armor gift pokemon
  2. Wooden american flag cross
  3. Physicians formula healthy powder
  4. Ancient rome vocabulary review crossword
  5. Golf cart brakes lock up

Data science is nothing without data. Yes, that’s obvious. What is not so obvious is the series of steps involved in getting the data into a format which allows you to explore the data. You may be in possession of a dataset in CSV format (short for comma-separated values) but no idea what to do next. This post will help you get started in data science by allowing you to load your CSV file into Colab.

Colab (short for Colaboratory) is a free platform from Google that allows users to code in Python. Colab is essentially the Google Suite version of a Jupyter Notebook. Some of the advantages of Colab over Jupyter include an easier installation of packages and sharing of documents. Yet, when loading files like CSV files, it requires some extra coding. I will show you three ways to load a CSV file into Colab and insert it into a Pandas dataframe.

(Note: there are Python packages that carry common datasets in them. I will not discuss loading those datasets in this article.)

To start, log into your Google Account and go to Google Drive. Click on the New button on the left and select Colaboratory if it is installed (if not click on Connect more apps, search for Colaboratory and install it). From there, import Pandas as shown below (Colab has it installed already).

import pandas as pd

1) From Github (Files < 25MB)

The easiest way to upload a CSV file is from your GitHub repository. Click on the dataset in your repository, then click on View Raw. Copy the link to the raw dataset and store it as a string variable called url in Colab as shown below (a cleaner method but it’s not necessary). The last step is to load the url into Pandas read_csv to get the dataframe.

url = 'copied_raw_GH_link'df1 = pd.read_csv(url)# Dataset is now stored in a Pandas Dataframe

2) From a local drive

To upload from your local drive, start with the following code:

from google.colab import files
uploaded = files.upload()

It will prompt you to select a file. Click on “Choose Files” then select and upload the file. Wait for the file to be 100% uploaded. You should see the name of the file once Colab has uploaded it.

Finally, type in the following code to import it into a dataframe (make sure the filename matches the name of the uploaded file).

import iodf2 = pd.read_csv(io.BytesIO(uploaded['Filename.csv']))# Dataset is now stored in a Pandas Dataframe

3) From Google Drive via PyDrive

This is the most complicated of the three methods. I’ll show it for those that have uploaded CSV files into their Google Drive for workflow control. First, type in the following code:

# Code to read csv file into Colaboratory:!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials# Authenticate and create the PyDrive client.
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

When prompted, click on the link to get authentication to allow Google to access your Drive. You should see a screen with “Google Cloud SDK wants to access your Google Account” at the top. After you allow permission, copy the given verification code and paste it in the box in Colab.

Once you have completed verification, go to the CSV file in Google Drive, right-click on it and select “Get shareable link”. The link will be copied into your clipboard. Paste this link into a string variable in Colab.

link = 'https://drive.google.com/open?id=1DPZZQ43w8brRhbEMolgLqOWKbZbE-IQu' # The shareable link

What you want is the id portion after the equal sign. To get that portion, type in the following code:

fluff, id = link.split('=')print (id) # Verify that you have everything after '='

Finally, type in the following code to get this file into a dataframe

downloaded = drive.CreateFile({'id':id})
df3 = pd.read_csv('Filename.csv')# Dataset is now stored in a Pandas Dataframe

Final Thoughts

These are three approaches to uploading CSV files into Colab. Each has its benefits depending on the size of the file and how one wants to organize the workflow. Once the data is in a nicer format like a Pandas Dataframe, you are ready to go to work.

Bonus Method — My Drive

Thank you so much for your support. In honor of this article reaching 50k Views and 25k Reads, I’m offering a bonus method for getting CSV files into Colab. This one is quite simple and clean. In your Google Drive (“My Drive”), create a folder called data in the location of your choosing. This is where you will upload your data.

From a Colab notebook, type the following:

from google.colab import drive

Just like with the third method, the commands will bring you to a Google Authentication step. You should see a screen with Google Drive File Stream wants to access your Google Account. After you allow permission, copy the given verification code and paste it in the box in Colab.

In the notebook, click on the charcoal > on the top left of the notebook and click on Files. Locate the data folder you created earlier and find your data. Right-click on your data and select Copy Path. Store this copied path into a variable and you are ready to go.

path = "copied path"
df_bonus = pd.read_csv(path)# Dataset is now stored in a Pandas Dataframe

What is great about this method is that you can access a dataset from a separate dataset folder you created in your own Google Drive without the extra steps involved in the third method.

Sours: https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92
How to import dataset from local machine to Google Colab ? step-by-step method

Importing Data to Google Colab — the CLEAN Way

In this article I will present:

  • An introduction of Google Colab
  • 2 much-used “quick and dirty” methods to upload data to Colab
  • 2 automated, “clean” methods to upload data to Colab

It is still hard to believe, but it is true. We can run heavy data science notebooks for free on Google Colab.

Colab is a Cloud service, which means that a server at Google will run the notebook rather than your own, local computer.

Maybe even more surprising is that the hardware behindit is quite good!

There is one big issue with Google Colab, often discussed before, which is the storage of your data. Notebooks, for example, Jupyter notebooks, often use data files stored locally, on your computer. This is often done using a simple read_csv statement or comparable.

The Cloud’s local is not your local.

But Google Colaboratory is running in the Cloud. The Cloud’s local is not your local. Therefore a read_csv statement will search for the file on Google’s side rather than on your side. And then it will not find it.

To get your data into your Colab notebook, I first discuss the two most known methods, together with their advantages and disadvantages. After that, I discuss two alternative solutions, that can be more appropriate especially when your code has to be easy to industrialize.

Manual Method 1 — using files.upload() to upload data to Colab

  1. Using files.upload() directly in the Colab notebook gives you a traditional upload button that allows you to move files from your computer into to the Colab environment.

2. Then you use io.StringIO() together with pd.read_csv to read the uploaded file into a data frame

Advantage of using files.upload() to upload data to Colab:
This is the easiest approach of all, even though it requires a few lines of code.

Disadvantages of using files.upload() to upload data to Colab:
For large files, the upload might take a while. And then whenever the notebook is restarted (for example if it fails or other reasons…), the upload has to be redone manually. This is not the best solution, because firstly our code wouldn’t re-execute automatically when relaunched and secondly it requires tedious manual operations in case of notebook failures.

Manual Method 2 — Mounting your Google Drive onto Colab

Upload your data to Google Drive before getting started with the notebook. Then you mount your Google Drive onto the Colab environment: this means that the Colab notebook can now access files in your Google Drive.

  1. Mount your drive using drive.mount()

2. Access anything in your Google Drive directly

Advantages of mounting your Google Drive onto Colab:
This is also quite easy. Google Drive is very user-friendly and uploading your data to Google Drive is no problem for most people. Also, once the upload is done, it does not require manual reloading when restarting the notebook. So it’s better than approach 1.

Disadvantages of mounting your Google Drive onto Colab:
The main disadvantage I see from this approach is mainly for company / industrial use. As long as you’re working on relatively small projects, this approach is great. But if access management and security are at stake, you will find that this approach is difficult to industrialize.

Also, you may not want to be in a 100% Google Environment, as multi-cloud solutions give you more independence from different Cloud vendors.

If your project is small, and if you know that it will always remain only a notebook, previous approaches can be acceptable. But for any project that may grow larger in the future, separating data storage from your notebook is a good step towards a better architecture.

If you want to move towards a cleaner architecture for data storage in your Google Colab notebook, try going for a proper Data Storage solution.

There are many possibilities in Python to connect with data stores. I here propose two solutions: AWS S3 for file storage and SQL for relational database storage:

Clean method 1 — connect an AWS S3 bucket

S3 is AWS’s file storage, which has the advantage of being very similar to the previously described ways of inputting data to Google Colab. If you are not familiar with AWS S3, don’t hesitate to have a look over here.

Accessing S3 file storage from Python is very clean code and very performant. Adding authentification is possible.

Advantages of using S3 with Colab:
S3 is taken seriously as a data storage solution by the software community, while Google Drive, though more appreciated for individual users, is preferred by many developers only for the integration with other Google Services.

This approach, therefore, improves both your code and your architecture!

Disadvantages of using S3 with Colab:
To apply this method, you will need to use AWS. It is easy, but it may still be a disadvantage in some cases (e.g. company policy). Also, it may take time to load the data every time. It can be longer than loading from Google Drive since the data source is separate.

Clean Method 2 — connect an SQL Database to Colab

If you have data already in a relational database like MySQL or other, it would also be a good solution to plug your Colab notebook directly to your database.

SQLAlchemy is a package that allows you to send SQL queries to your relational database and this will allow to have well-organized data in this separate SQL environment while keeping only your Python operations in your Colab notebook.

Advantages of connecting an SQL Database to Colab:
This is a good idea when you are starting to get to more serious applications and you want to have already a good data storage during your development.

Disadvantages of connecting an SQL Database to Colab:
It will be impossible to use Relational Data Storage with unstructured data, but a nonrelational database may be the answer in this case. A more serious problem can be the query execution time in case of very large volumes. It can also be a burden to manage the database (if you don’t have one or if you cannot easily share access).

Google Colab notebooks are great but it can be a real struggle to get data in and out.

Google Colab notebooks are great but it can be a real struggle to get data in and out.

Importing data by Manual Upload or Mounting Google Drive are both easy to use but difficult to industrialize. Alternatives like AWS S3 or a Relational database will make your system less manual and therefore better.

The 2 manual methods are great for small short-term projects and the two methods with external storage should be used when a project needs a clean data store.

Think through your architecture before it’s too late!

Each method has its advantages and disadvantages and only you can decide which one fits with your use case. Whatever storage you use, but be sure to think through your architecture before it’s too late!

I hope this article will help you with building your projects. Stay tuned for more and thanks for reading!

Sours: https://towardsdatascience.com/importing-data-to-google-colab-the-clean-way-5ceef9e9e3c8

Google upload data colab to

It’s one of the first hurdles you run into when you use Google Colab: how do I get my data in there? A good question, because other cloud notebook solutions (like Azure Notebooks) allow you to upload your files through the interface. Google Colab does not, but its deep integration with Google Drive offers opportunities. This blog post helps you get this solved in no time.

I will elaborate on the three most convenient options: uploading the file, using PyDrive and mounting your Google Drive.

Option 1: Upload it

The first solution is pretty straightforward. By using files from the google.colab package, you can manually select upload files from your computer to your notebook kernel’s local variables. Keep in mind that if your kernel is restarted, you’ll have to reupload the files again.

from google.colab import files import pandas as pd uploaded = files.upload() pd.read_csv(io.StringIO(uploaded['train.csv'].decode('utf-8')))

Clearly, this is a quick and dirty solution. If you plan on working on a project for a couple of weeks, this might not be the best option for you.

Option 2: Use PyDrive and Google Drive

PyDrive is a wrapper for the Python Google Drive API. It offers many functionalities, including interacting with files that are stored in Google Drive.

When you choose to use PyDrive and run the following code, you’ll be redirected to an authentication page, which will return a key that you can use to identify yourself within Colab. Once you paste the key in the input field in Colab, you can copy files from your Google Drive and use them in your kernel.

import pandas as pd from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials # Authenticate with Google auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth) def read_csv_from_drive(file_id, file_name): dl = drive.CreateFile({'id': file_id}) dl.GetContentFile(file_name) return pd.read_csv(file_name) train = read_csv_from_drive('<file_id>', 'train.csv')

To download files to your kernel, you’ll need to know the file ID. The easiest way is to generate a sharing link and get it from the returned URL.

I really like this solution, myself. Because it is reproducible you only need to map the files once. This is especially useful if you’re dealing with a multitude of files. Even more importantly: you can share the files with your collaborators and they too will be able to access them and properly run the notebook.

But there’s a drawback. If you’re working with huge files, you might not want to download the files from your Drive to your kernel. It could take a while. That’s why there’s a third solution.

Option 3: mount your drive

The third and final solution is to mount your complete Google Drive to the kernel. This way, your Google Drive will be treated like it’s a local disk in your kernel. It’s really easy:

from google.colab import drive drive.mount('/content/drive') pd.read_csv('<path>')

To get the path to a file, you simply copy the path from the file explorer in Colab on the left-hand side of the interface.

By the way, you can also mount your drive with the click of a button now.

Just like the PyDrive method, it’s reproducible (for yourself) and is a good way to handle a lot of files. The biggest drawback here is that I currently don’t see how you can share files between your collaborators.


Quick and dirtyyesnono
Many filesnoyesyes
Huge filesnoyes, but noyes

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Sours: https://www.roelpeters.be/how-to-uploading-files-in-google-colab/
How to Read Dataset in Google Colab from Google Drive

Import data into Google Colaboratory

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



if you need to upload data from local drive:

execute and this will display a choose file button - find your upload file - click open

After uploading, it will display:


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:

Copy - '29PGh8XCts3mlMP6zRphvnIcbv27boawn' - that is the file ID.

In your 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. ):

Now you can print to see the data is there:

Sours: https://stackoverflow.com/questions/46986398/import-data-into-google-colaboratory

You will also like:

He held her wide hips and slowly plunged his penis between her chic buttocks into her wet pussy. Katya, lowering her head, silently endured his tremors. Anatole now and then pulled out the member, wet from the discharge of his daughter-in-law, and again plunged it into the open vagina.

1838 1839 1840 1841 1842