Pranathi
Pranathi

Reputation: 831

Exporting Data from google colab to local machine

How to export data frames which are created in google colab to your local machine?

I have cleaned a data set on google colab. Now I want to export the data frame to my local machine. df.to_csv is saving file to the virtual machine and not my local machine.

Upvotes: 71

Views: 190179

Answers (4)

iacob
iacob

Reputation: 24231

Downloading files to your local file system

files.download will invoke a browser download of the file to your local computer.

from google.colab import files

with open('example.txt', 'w') as f:
  f.write('some content')

files.download('example.txt')

Upvotes: 4

korakot
korakot

Reputation: 40838

Try this

from google.colab import files
files.download("data.csv")

Update(Sep 2018): now it's even easier

  • open the left pane
  • select 'Files' tab
  • click 'Refresh'
  • right click the file, then download

Update (Jan 2020): the UI changes

  • click on the folder icon on the left pane (3rd icon)
  • click 'Refresh'
  • right click the file, then download

Upvotes: 86

Abhishek Thombre
Abhishek Thombre

Reputation: 631

Try this:

First you can save the file using pandas to_csv functionality later on you can download that file using google colab files functionality.

from google.colab import files
df.to_csv('filename.csv') 
files.download('filename.csv')

Upvotes: 52

mike
mike

Reputation: 951

You can download the csv to your associated google drive. First you must install PyDrive.

!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from google.colab import files
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

This will generate a token in a browser for you to then paste into an input box that will be shown in your notebook.

Save your pandas data frame df.to_csv('mydataframe.csv', sep='\t')

To keep things neat you can create a new folder in your drive and then use the following:

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() for file1 in file_list: print('title: %s, id: %s' % (file1['title'], file1['id']))

which will list the files and folders in your google drive and their id that you will need for the following step.

file = drive.CreateFile({'parents':[{u'id': 'id of folder you want to save in'}]}) file.SetContentFile("mydataframe.csv") file.Upload()

It will now be in your google drive in the given folder.

Upvotes: 1

Related Questions