How to read a csv file from a local disk into a dataframe?

I am trying to read a csv file from my local disk C to create a dataframe:
mypath = ‘C:\Users\LENOVO\Desktop\ kiva_data.csv’
df = pd.read_csv(mypath)
print(df.head())
but I am always getting an error that it doesn’t exist.
What is wrong with my code above?
Given that the path is correct, and the csv file exists there, of course.

@css1989329549
Just make sure the CSV file is in the same working directory as your code.

not relevant for an absolute path, and for a relative path it’s not the code file’s location that matters, but the location where it is executed

and, the path is probably wrong, which is why there’s an error saying there’s nothing in that location… there isn’t, if one for example copied that path and pasted it into a text editor, it too would find nothing. so then the solution would be to obtain the path for the desired file and paste it into the code, or type it in if able to do so exactly (which is probably the problem, not having typed it exactly as it is)

@ionatan
I understand. But don’t you agree that the best way to avoid there kinda errors is to put everything in the same directory so that you won’t have to define the path.

no I don’t, however I would agree with something further in that direction, which is to not specify any particular file and instead provide either text or a filename when running the program

windows users in particular are not accustomed to telling programs about other content, and instead just “click” a program to run it, so they end up hard-coding a specific file

No, actually it is not in the same folder and I cannot put it here. I work on IBM Watson Studio, and I just uploaded the ipynb with the project and I don’t know how to upload also the csv file.
Then probably another my question is: how can I add the csv file in the same folder on IBM Watson?
I tried to put it as an asset type ‘Data’ in the same project folder, but nothing changed.

your path is wrong, there is nothing else to it

I’d tell you how it’s likely wrong but it’s a simple enough thing that once you know it (from the error message and to some degree me confirming it) … you should be able to get it right, it’s just a matter of copy-pasting a path to the file and if you do that without modifying it in the process then you’ll have a correct path. there’s nothing quirky or surprising, you typed it wrong, that’s all.

Read the docs:
https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/load-and-access-data.html#load-data-from-local-files

It helped, I found the solution! Not the easiest way, though, but it least it worked. Thanks a lot!

1 Like

Uhm, but the file is exactly in the folder of the path I wrote. I checked it already many times. I really cannot understand what else can be wrong with the path, probably I should change slash symbols to the opposite, as I read in some forums on Google. Well, I am quite a beginner in Python, so still authorized to this kind of questions :smiley:

Are you sure the path isn’t actually:

C:\Users\LENOVO\Desktop\kiva_data.csv

?

You’re not using that path when you’re checking, you’re (likely) using your graphical file explorer, clicking your way through. That file explorer will not show things that are not there, you’re only clicking on valid things.

A better measurement would be to copy the path you have in your code, and ask a text editor to edit that file.

So you have modifications going on in both directions. When you type the path into your code you’re slightly modifying it and when you’re checking whether the file exists you’re modifying the path again, back to the correct one.

@css1989329549,

I directed you to the IBM Watson Studio docs because I’ve never used IBM Watson Studio and I wasn’t sure if you were using it on the cloud or locally.

That being said, whenever you are using Windows paths, you should be wary of the backslashes (\). In many programming languages, a backslash acts as an escape character. So, when specify the path, you should do one of the following:

  • Replace your backslashes with forward slashes (/) (preferred method); or
  • You can put two backslashes instead of one in order to “escape the escape,” if you will. For example, your path could be written as 'C:\\Users\\LENOVO\\Desktop\\kiva_data.csv'

Also, as @midlindner pointed out, you had what looked like an extra space in your path before your file name.

Yes, you are right, thank you! There was an extra space. Now I corrected it and this time double checked everything, but still have the same error.

maybe when you say local you actually mean remote