web-dev-qa-db-ja.com

データフレームをcsvファイルとしてGoogle ColabからGoogleドライブにエクスポートする

データフレームをcvvとしてcolabからgoogle driveにアップロードしたいのですが、うまくいきませんでした。単純なテキストファイルをアップロードできますが、csvをアップロードできませんでした。

私は次のコードを試しました:

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()
9
Athar Noraiz

!cpコマンドを使用しない

_from google.colab import drive_

  • GoogleドライブをColabノートブックにマウントします

drive.mount('/drive')

  • アップロードする前に、Googleドライブにフォルダ名が作成されていることを確認してください

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

2
IndPythCoder

Pandasを使用したくない場合は、次のようにします。

df_mini.coalesce(1)\
   .write\
   .format('com.databricks.spark.csv')\
   .options(header='true', delimiter='\t')\
   .save('gdrive/My Drive/base_mini.csv')
1
lfvv
# 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)
1
Justin Braaten