【问题标题】:Cant save / Export my Dataframe to CSV on Google's COLAB无法在 Google COLAB 上将我的数据框保存/导出为 CSV
【发布时间】:2020-02-26 10:16:09
【问题描述】:

我在谷歌的合作实验室 COLAB 中有一个 Panda Dataframe。我正在尝试将其作为 CSV 文件导出到我的 GoogleDrive,但它不起作用。这是我使用的代码:

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

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

此时,我收到消息:

 Mounted at /content/drive

然后我继续:

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

这是错误:

OSError:[Errno 107] 传输端点未连接:'/gdrive'

我将它用于不同的浏览器,包括 Chrome,但并没有什么不同。我不确定是什么问题。我愿意接受任何可以帮助我将 DataFrame 导出为 gdrive 或本地文件的解决方案。

谢谢!!

【问题讨论】:

标签: python pandas tensorflow cloud google-colaboratory


【解决方案1】:

感谢@Roozeppe 和@Trenton McKinney。这对我有用:

# 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

“我的云端硬盘”/

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

【讨论】:

    【解决方案2】:

    从目录中删除content/

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

    变成

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

    通过运行pwd 检查您的工作目录。除非您更改它,否则它应该是 /content,默认值。假设它是默认的,只需运行MyDF.to_csv('drive/My Drive/MyFolders/MyDF.csv')

    【讨论】:

    • 谢谢!工作目录未设置在应有的位置。我将在下面发布最终的代码。
    猜你喜欢
    • 2019-05-22
    • 2021-05-17
    • 2022-08-19
    • 2020-11-28
    • 1970-01-01
    • 2022-01-07
    • 2019-03-06
    • 2016-12-01
    • 2020-01-25
    相关资源
    最近更新 更多