2

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

Meisam H
  • 67
  • 1
  • 8
  • Does this answer your question? [How can I download a pandas Dataframe in Google Colab?](https://stackoverflow.com/questions/48854943/how-can-i-download-a-pandas-dataframe-in-google-colab) – Trenton McKinney Oct 31 '19 at 04:19
  • Thanks @https://stackoverflow.com/users/7758804/trenton-mckinney. Couldn't get the second line to work. The new error message is: `MessageError: TypeError: Failed to fetch'. I am trying to manual navigate to the folder and save there. Will update if it works. in the meantime, any other suggestions are welcome. – Meisam H Oct 31 '19 at 04:36

2 Answers2

2

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')
Meisam H
  • 67
  • 1
  • 8
1

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

Roozeppé
  • 11
  • 3
  • Thanks! The working directory was not set where it should. I will post the final code that work below. – Meisam H Nov 11 '19 at 18:18