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

13

6

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.

Blanca answered 8/4, 2017 at 17:36 Comment(1)
Your path should start with a / (the root of your filesystem), as in /Users/user.... Otherwise, you are looking for a Users directory in your current working directory.Daylong
N
9

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.

Nocturn answered 5/2, 2018 at 18:46 Comment(0)
C
3

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
Cominform answered 8/4, 2017 at 17:39 Comment(1)
Thank you, and I was able to get it to work now. And I jumped into python without learning the basics, so this is good information to know. Thank you again. :)Blanca
F
1

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()
Friendly answered 21/10, 2018 at 23:14 Comment(0)
T
1

I solved this issue by moving the csv files I wanted to use into a folder. After this on the jupyter home page, I moved the folder holding the csv files into the SAME folder my jupyter notebook was in. I simply dragged the csv folder into the same folder my jupyter notebook was in.

Turnbull answered 23/8 at 20:12 Comment(0)
D
0

The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules.

import os
import pandas as pd
os.chdir("c:\Pandas")
df=pd.read_csv("names.csv")
df

This might help. :)

Duquette answered 25/1, 2019 at 10:22 Comment(0)
H
0

The file name is case sensitive, so check your case.

Hedberg answered 25/6, 2019 at 18:17 Comment(0)
W
0

I had the same problem on a Mac too and for some reason it only happened to me there. And I tried to use many tricks but nothing works. I recommend you go directly to the file, right click and then press “alt” key after that the option to “copy route” will appear, and just paste it into your jupyter. For some reason that worked to me.

Westlund answered 23/9, 2019 at 5:55 Comment(0)
R
0

I believe the issue is that you're not using fully qualified paths. Try this:

Move the data into a suitable project directory. You can do this using the %%bash Magic commands.

%%bash
mkdir -p /project/data/
cp data.csv /project/data/data.csv

You can read the file

f = open("/project/data/data.csv","r")
print(f.read())
f.close()

But it might be most useful to load it into a library.

import pandas as pd
data = pd.read_csv("/project/data/data.csv")

I’ve created a runnable Jupyter notebook with more details here: Jupyter Basics: Reading Files.

Rum answered 23/9, 2019 at 15:24 Comment(0)
E
0

Try double quotes, instead of single quotes. it worked for me.

Ecclesiology answered 21/11, 2019 at 18:32 Comment(0)
F
0

you can open csv files in Jupyter notebook by following these easy steps- Step 1 - create a directory or folder (you can also use old created folder) Step 2 - Change your Jupyter working directory to that created directory -

import os
os.chdir('D:/datascience/csvfiles')

Step 3 - Now your directory is changed in Jupyter Notebook. Store your file(s) in that directory. Step 4 - Open your file -

import pandas as pd

df = pd.read_csv("workbook1.csv")

Now your file is read and stored in a Data Frame variable df, you can display this file content by following

  1. df.head() - display first five rows of this file
  2. df - display all rows of this file

Happy Data Science!

Friesian answered 18/9, 2020 at 13:55 Comment(0)
B
0

There was a similar problem for me while reading a CSV file in Jupyter notebook from the computer.

I solved it by substituting the "" symbol with "/" in the path like this.

This is what I had:

"C:\Users\RAJ\Desktop\HRPrediction\HRprediction.csv"

This is what I changed it for:

"C:/Users/RAJ/Desktop/HRPrediction/HRprediction.csv".

Butanol answered 6/4, 2021 at 17:24 Comment(0)
E
0

This is what worked for me. I am using Mac OS.

Save your CSV on a separate folder on your desktop.

When opening a Jupyter notebook press on the same folder that your dataset is currently saved in. Press new notebook in the upper right hand corner.

After opening a new notebook. Code as per usual and read your data using import pandas as pd and pd.read_csv calling to your dataset.

Exegesis answered 27/6, 2021 at 14:53 Comment(0)
R
0

 No need to use anything extra just use r in front of the location.

df = pd.read_csv(r'C:/Users/owner/Desktop/contacts.csv'

Royal answered 14/10, 2022 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.