Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Jupyter Notebook - Unable to open CSV file through a path

Tags:

python

pandas

csv

I am a little new to Python, and I have been using the Jupyter Notebook through Anaconda. I am trying to import a csv file to make a DataFrame, but I am unable to import the file.

Here is an attempt using the local method:

df = pd.read_csv('Workbook1')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-11-a2deb4e316ab> in <module>()
----> 1 df = pd.read_csv('Workbook1')

After that I tried using the path (I put user for my username)

df = pd.read_csv('Users/user/Desktop/Workbook1.csv')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-13-3f2bedd6c4de> in <module>()
----> 1 df = pd.read_csv('Users/user/Desktop/Workbook1.csv')

I am using a Mac, which I am also new to, and I am not 100% sure if I am correctly importing the right path. Can anyone offer some insight or solutions that would allow me to open this csv file.

like image 288
XxEthan70xX Avatar asked Apr 08 '17 17:04

XxEthan70xX


People also ask

How do I get a csv file to read a path?

Full file paths require describing the path from the root context. In windows this would be providing a path such as f'C:\Users\mycsvfolder\{ColumnName}. csv' . Providing the full path to to_csv() will have the file written there.

Why is my file not opening in Jupyter Notebook?

Jupyter doesn't load or doesn't work in the browserTry disabling any browser extensions and/or any Jupyter extensions you have installed. Some internet security software can interfere with Jupyter. If you have security software, try turning it off temporarily, and look in the settings for a more long-term solution.


3 Answers

Instead of providing path, you can set a path using the code below:

import os
import pandas as pd
os.chdir("D:/dataset")
data = pd.read_csv("workbook1.csv")

This will surely work.

like image 111
RATAN KUMAR Avatar answered Nov 13 '22 19:11

RATAN KUMAR


Are you sure that the file exists in the location you are specifying to the pandas read_csv method? You can check using the os python built in module:

import os
os.path.isfile('/Users/user/Desktop/Workbook1.csv')

Another way of checking if the file of interest is in the current working directory within a Jupyter notebook is by running ls -l within a cell:

ls -l
like image 36
mgig Avatar answered Nov 13 '22 19:11

mgig


I think the problem is probably in the location of the file:

df1 = pd.read_csv('C:/Users/owner/Desktop/contacts.csv')

Having done that, now you can play around with the big file if you have, and create useful data with:

df1.head()
like image 35
Sham Pat Avatar answered Nov 13 '22 19:11

Sham Pat