overview datatourisme
overview datatourisme

Reputation: 41

How to actually save a csv file to google drive from colab?

so, this problem seems very simple but apparently is not. I need to transform a pandas dataframe to a csv file and save it in google drive.

My drive is mounted, I was able to save a zip file and other kinds of files to my drive. However, when I do:

df.to_csv("file_path\data.csv")

it seems to save it where I want, it's on the left panel in my colab, where you can see all your files from all your directories. I can also read this csv file as a dataframe with pandas in the same colab.

HOWEVER, when I actually go on my Google Drive, I can never find it! but I need a code to save it to my drive because I want the user to be able to just run all cells and find the csv file in the drive.

I have tried everything I could find online and I am running out of ideas! Can anyone help please?

I have also tried this which creates a visible file named data.csv but i only contains the file path

import csv
with open('file_path/data.csv', 'w', newline='') as csvfile:
  csvfile.write('file_path/data.csv')

HELP :'(

edit :

import csv

with open('/content/drive/MyDrive/Datatourisme/tests_automatisation/data_tmp.csv') as f:
    s = f.read()

with open('/content/drive/MyDrive/Datatourisme/tests_automatisation/data.csv', 'w', newline='') as csvfile:
  csvfile.write(s)

seems to do the trick.

  1. First export as csv with pandas (named this one data_tmp.csv),
  2. then read it and put that in a variable,
  3. then write the result of this "reading" into another file that I named data.csv,

this data.csv file can be found in my drive :)

HOWEVER when the csv file I try to open is too big (mine has 100.000 raws), it does nothing. Has anyone got any idea?

Upvotes: 4

Views: 16603

Answers (3)

Javier Huerta
Javier Huerta

Reputation: 128

I recommend you to use pandas to work with data in python, works very well.

In that case, here is a simple tutorial, https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html Pandas tutorial

Then to save your data frame to drive, if you have your drive already mounted, use the function to_csv

dataframe.to_csv("/content/drive/MyDrive/'filename.csv'", index=False)

The above code will do the trick

Upvotes: 0

nidzytryingtocode
nidzytryingtocode

Reputation: 87

  1. First of all, mount your Google Drive with the Colab:
from google.colab import drive
    
drive.mount('/content/drive')
  1. Allow Google Drive permission
  2. Save your data frame as CSV using this function:
import pandas as pd

filename = 'filename.csv'

df.to_csv('/content/drive/' + filename)

In some cases, directory '/content/drive/' may not work, so try 'content/drive/MyDrive/'

Hope it helps!

Upvotes: 4

Mohana
Mohana

Reputation: 489

Here:

df.to_csv( "/Drive Path/df.csv", index=False, encoding='utf-8-sig')

Upvotes: 1

Related Questions