简体   繁体   English

无法在 Google 的 COLAB 上保存/导出我的 Dataframe 到 CSV

[英]Cant save / Export my Dataframe to CSV on Google's COLAB

I have a Panda Dataframe in google's colaboratory, COLAB.我在谷歌的合作实验室 COLAB 中有一个 Panda Dataframe。 I am trying to export it as a CSV file to my GoogleDrive and it doesn't work.我正在尝试将它作为 CSV 文件导出到我的 GoogleDrive,但它不起作用。 Here is the code I use:这是我使用的代码:

d = {'col1': [1, 2], 'col2': [3, 4]}
MyDF = pd.DataFrame(data=d)

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

at this point, I get the message:此时,我收到消息:

 Mounted at /content/drive

Then I proceed with:然后我继续:

MyDF.to_csv('content/drive/My Drive/MyFolders/MyDF.csv')

Here is the error:这是错误:

OSError: [Errno 107] Transport endpoint is not connected: '/gdrive' OSError:[Errno 107] 传输端点未连接:'/gdrive'

I used it with different browsers, including Chrome, and it didn't made a difference.我将它与不同的浏览器一起使用,包括 Chrome,但并没有什么不同。 I am not sure what is the problem.我不确定是什么问题。 I am open to any solutions that can help me export my DataFrame as a file on my gdrive or locally.我愿意接受任何可以帮助我将 DataFrame 导出为 gdrive 或本地文件的解决方案。

Thank you!!谢谢!!

Thanks to @Roozeppe and @Trenton McKinney.感谢@Roozeppe 和@Trenton McKinney。 This is what worked for me:这对我有用:

# Mounting the gdrive
from google.colab import drive
drive.mount('/content/drive')

# getting a list of Directories show I am not where I should be
ls

'My Drive'/ “我的驱动器”/

# Changing the working Directory to Where I want to Export. 
# The space in 'My Drive' Name didn't create any issues.


%cd /gdrive/My Drive/PythonExports

# Exporting MyDf dataframe

MyDF.to_csv('MyDF.csv')

Remove content/ from the directory从目录中删除content/

MyDF.to_csv('content/drive/My Drive/MyFolders/MyDF.csv')

becomes变成

MyDF.to_csv('drive/My Drive/MyFolders/MyDF.csv')

Check your working directory by running pwd .通过运行pwd检查您的工作目录。 Unless you changed it, it should be /content , the default.除非您更改它,否则它应该是/content ,默认值。 Assuming it's the default, just run MyDF.to_csv('drive/My Drive/MyFolders/MyDF.csv')假设它是默认值,只需运行MyDF.to_csv('drive/My Drive/MyFolders/MyDF.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM