Search code examples
pythonpandasgoogle-colaboratory

Unable to read csv file in Google Colab


I imported these libraries and trying to read a csv file on my desktop

import pandas as pd
import numpy as np

df = pd.read_csv('/Users/yoshithKotla/Desktop/canal/verymimi_M.csv')

But I get an error saying

FileNotFoundError: [Errno 2] No such file or directory: '/Users/yoshithKotla/Desktop/canal/verymimi_M.csv'

The path of the file is correct and it is present on my desktop.


Solution

  • You cannot read the local files present on your computer directly into the google colab environment.

    To upload from your local drive, start with the following code:

    from google.colab import files
    uploaded = files.upload()
    

    It will prompt you to select a file. Click on “Choose Files” then select and upload the file. Wait for the file to be 100% uploaded. You should see the name of the file once Colab has uploaded it.

    Finally, type in the following code to import it into a dataframe (make sure the filename matches the name of the uploaded file).

    import iodf2 = pd.read_csv(io.BytesIO(uploaded['Filename.csv']))# Dataset is now stored in a Pandas Dataframe
    

    Reference: https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92