Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export dataframe as csv file from google colab to google drive

Tags:

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() 
like image 659
Athar Noraiz Avatar asked Dec 22 '18 19:12

Athar Noraiz


People also ask

How do I import dataset from Google colab to Google Drive?

Loading a Dataset from the Google Drive to Google Colab Click the link given. Then you'll see the usual google account authentication steps and after two steps you'll see authentication keys for your google drive. Now add it in the early appeared shell and press enter. Now your google drive is mounted.


2 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/" 
like image 69
korakot Avatar answered Sep 17 '22 13:09

korakot


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

like image 37
IndPythCoder Avatar answered Sep 18 '22 13:09

IndPythCoder