简体   繁体   中英

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

I have a Panda Dataframe in google's colaboratory, COLAB. I am trying to export it as a CSV file to my GoogleDrive and it doesn't work. 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'

I used it with different browsers, including Chrome, and it didn't made a difference. 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.

Thank you!!

Thanks to @Roozeppe and @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

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 . Unless you changed it, it should be /content , the default. Assuming it's the default, just run MyDF.to_csv('drive/My Drive/MyFolders/MyDF.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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