Eric S
Eric S

Reputation: 135

Importing CSV file into Google Colab using numpy loadtxt

I'm trying to migrate a JupyterLab notebook to Google Colab. In JupyterLab, when I have the notebook file and the associated csv files in the same directory, it is easy to import the data using numpy's loadtxt function as follows:

import numpy as np
filein = "testfile.csv"
data = np.loadtxt(open(filein, "rb"), delimiter=",", skiprows=1)

For various reasons I'd like to continue to use np.loadtxt in Colab. However when I try the same code there, it can't find the csv file despite it residing in the same Google Drive location as the notebook file. I get this error: "FileNotFoundError: [Errno 2] No such file or directory: 'testfile.csv'".

I gather I somehow need to provide a path to the file, but haven't been able to figure out how to do this. Is there any straightforward way to use np.loadtxt?

Upvotes: 7

Views: 9225

Answers (3)

steunet
steunet

Reputation: 21

Shorter and without command

# mount gdrive with this code
from google.colab import drive
drive.mount('/content/drive')
#below where the file is in gdrive, change with your
data_path = "/content/drive/My Drive/Colab Notebooks/test/"
yearsBase, meanBase = np.loadtxt(data_path + 'file.csv', delimiter=',', unpack=True)

done, no other code needed ciao

Upvotes: 2

akilat90
akilat90

Reputation: 5696

Here's another way that has lesser manual intervention. This is more useful if you intend to run the colab notebook for a long time across multiple disconnected sessions so that you don't need to manually upload the file every time.

  1. Upload the text file to google drive. Click share and obtain the shareable link. For example, this is an example shareable link for the file iris.csv: https://drive.google.com/file/d/1Llp483f91dAJriuE6PanmecLA9sWDPyi/view

  2. Copy the file ID from the above link. In this case, it is 1Llp483f91dAJriuE6PanmecLA9sWDPyi

  3. Now you can download the file using the below cell in any colab notebook:

    file_id = "1Llp483f91dAJriuE6PanmecLA9sWDPyi" # replace with your ID
    !gdown https://drive.google.com/uc?id={file_id}
    

Type !ls to see the file in your workspace.

For a detailed official guide, refer this notebook: https://colab.research.google.com/notebooks/io.ipynb

Upvotes: 1

Bob Smith
Bob Smith

Reputation: 38579

Colab doesn't automatically mount Google Drive. By default, the working directory is /content on an ephemeral backend virtual machine.

To access your file in Drive, you'll need to mount it first using the following snippet:

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

Then, %cd /content/gdrive/My\ Drive to change the working directory to your Drive root. (Or, customize the path as needed to wherever testfile.csv is located.)

Upvotes: 12

Related Questions