Search code examples
pythonpandastensorflowcloudgoogle-colaboratory

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!!


Solution

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