Python 将数据从 google colab 导出到本地机器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49394737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting Data from google colab to local machine
提问by Pranathi
How to export data frames which are created in google colab to your local machine?
如何将在 google colab 中创建的数据帧导出到您的本地机器?
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.
我已经清理了 google colab 上的数据集。现在我想将数据框导出到我的本地机器。
df.to_csv
正在将文件保存到虚拟机而不是我的本地机器。
回答by korakot
Try this
尝试这个
from google.colab import files
files.download("data.csv")
Update(Sep 2018): now it's even easier
更新(2018 年 9 月):现在更容易了
- open the left pane
- select 'Files' tab
- click 'Refresh'
- right click the file, then download
- 打开左窗格
- 选择“文件”选项卡
- 点击“刷新”
- 右键单击文件,然后下载
Update (Jan 2020): the UI changes
更新(2020 年 1 月):UI 更改
- click on the
folder icon
on the left pane (3rd icon) - click 'Refresh'
- right click the file, then download
- 单击
folder icon
左侧窗格中的(第三个图标) - 点击“刷新”
- 右键单击文件,然后下载
回答by Abhishek Thombre
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.
首先,您可以to_csv
稍后使用 Pandas 功能保存文件,然后您可以使用 google colab 文件功能下载该文件。
from google.colab import files
df.to_csv('filename.csv')
files.download('filename.csv')
回答by mike
You can download the csv to your associated google drive. First you must install PyDrive.
您可以将 csv 下载到关联的谷歌驱动器。首先,您必须安装 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')
保存您的熊猫数据框
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']))
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.
这将列出您的谷歌驱动器中的文件和文件夹以及您在以下步骤中需要的 ID。
file = drive.CreateFile({'parents':[{u'id': '
id of folder you want to save in'}]}) file.SetContentFile("mydataframe.csv")
file.Upload()
file = drive.CreateFile({'parents':[{u'id': '
您要保存的文件夹的 ID'}]}) file.SetContentFile("mydataframe.csv")
file.Upload()
It will now be in your google drive in the given folder.
它现在将位于给定文件夹中的谷歌驱动器中。