Exporting Data from google colab to local machine

PythonGoogle Colaboratory

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

>### Downloading files to your local file system > files.download will invoke a browser download of the file to your local computer. > >python >from google.colab import files > >with open('example.txt', 'w') as f: > f.write('some content') > >files.download('example.txt') >

Solution 4 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPranathiView Question on Stackoverflow
Solution 1 - PythonkorakotView Answer on Stackoverflow
Solution 2 - PythonAbhishek ThombreView Answer on Stackoverflow
Solution 3 - PythoniacobView Answer on Stackoverflow
Solution 4 - PythonmikeView Answer on Stackoverflow