Export dataframe as csv file from google colab to google drive

Viewed 88371

I want to upload a dataframe as csv from colab to google drive.I tried a lot but no luck. I can upload a simple text file but failed to upload a csv.

I tried the following code:

import pandas as pd
df=pd.DataFrame({1:[1,2,3]})
df.to_csv('abc',sep='\t')
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
uploaded = drive.CreateFile({'title': 'sample.csv', 'mimeType':'csv'})
uploaded.SetContentFile('abc')
uploaded.Upload()
6 Answers

It may be easier to use mounting instead of pydrive.

from google.colab import drive
drive.mount('drive')

After authentication, you can copy your csv file.

df.to_csv('data.csv')
!cp data.csv "drive/My Drive/"

Without using !cp command

from google.colab import drive

  • Mounts the google drive to Colab Notebook

drive.mount('/drive')

  • Make sure the folder Name is created in the google drive before uploading

df.to_csv('/drive/My Drive/folder_name/name_csv_file.csv')

# Import Drive API and authenticate.
from google.colab import drive

# Mount your Drive to the Colab VM.
drive.mount('/gdrive')

# Write the DataFrame to CSV file.
with open('/gdrive/My Drive/foo.csv', 'w') as f:
  df.to_csv(f)

The other answers almost worked but I needed a small tweak:

from google.colab import drive
drive.mount('drive')

df.to_csv('/content/drive/My Drive/filename.csv', encoding='utf-8', index=False)

the /content/ bit proved necessary

If you don't want to work with Pandas, do this:

df_mini.coalesce(1)\
   .write\
   .format('com.databricks.spark.csv')\
   .options(header='true', delimiter='\t')\
   .save('gdrive/My Drive/base_mini.csv')

If you want to save locally then you can use this

f.to_csv('sample.csv')
from google.colab import files
files.download("sample.csv")
Related