+ 5

Problem in Python pandas read csv

Here is My code - import pandas as pd df = pd.read_csv(ā€œC:\users\user\Desktop\Inanā€) print(df.head()) However I keep getting unicode error. I put r,changed slashes,used decode utf,encoding utf,but none are working. They either show unicode error,or say file not found. What am I missing? Arenā€™t I supposed to put the file path of the csv file in my device inside the bracket? How do I make it work? Like I said,using double backlash,using different combination of slash,putting ā€˜rā€™ in front of C DID NOT WORK

7th Jul 2021, 6:37 PM
Ahnaf Inan
Ahnaf Inan - avatar
25 Respuestas
+ 4
I saved the file as ā€˜Deaths.csvā€™ So when I put the filepath,I was supposed to write ā€˜Deaths.csv.csvā€™ But instead I put only one csv. So I just changed the name to Deaths and put a csv in the code,and it was fixed. And I needed to put the r in front. It looks like this now - df = pd.read_csv(rā€œC:\Users\User\Desktop\Inan\deaths.csvā€) As this is an almost irreplicable and a silly mistake,I didnā€™t want to post the answer. But I did it anyways,just in case anyone might find it helpful XD
8th Jul 2021, 3:08 PM
Ahnaf Inan
Ahnaf Inan - avatar
+ 6
Have you tried double backslashes? pd.read.csv("C:\\users\\user\\Desktop\\Inan")
7th Jul 2021, 6:42 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Check the following things: 1) Is your file in the directory it is supposed to be? 2) Did you type the file name correctly in the Python script? 3) Is Python able to find the directory in which your file is? 4) Is the file closed (and not open in another program)?
7th Jul 2021, 7:21 PM
Lisa
Lisa - avatar
+ 3
Ahnaf Inan you want to read a certain file, don't you? So your path should be something like r"MyFolder\mydata.csv"
7th Jul 2021, 7:04 PM
Lisa
Lisa - avatar
+ 3
Ahnaf Inan no that mistake was not silly. It happens sometimes. To post the mistake upgrades this post and might help others.
8th Jul 2021, 5:09 PM
Oma Falk
Oma Falk - avatar
+ 2
import pandas as pd df = pd.read.csv(rā€œC:\users\user\Desktop\Inanā€) print(df.head()) also os.path can help.
7th Jul 2021, 6:40 PM
Oma Falk
Oma Falk - avatar
+ 2
My bad,that was a typo. I did run it with _csv,not .csv
7th Jul 2021, 6:51 PM
Ahnaf Inan
Ahnaf Inan - avatar
+ 2
You only pass the string with the directory? Where is the file name???
7th Jul 2021, 6:54 PM
Lisa
Lisa - avatar
+ 2
6. is content csv?
7th Jul 2021, 7:39 PM
Oma Falk
Oma Falk - avatar
+ 2
Oma Falk if youā€™re talking about what the error message I got before was,it was the unicode error,and the file not found error. The file not found error was fixed by properly writing the filename.csv. As for the unicode error,putting the r in front of C:\ fixed it by turning into a raw string
8th Jul 2021, 3:14 PM
Ahnaf Inan
Ahnaf Inan - avatar
+ 1
could u send a hardcooy of error message please?
7th Jul 2021, 7:21 PM
Oma Falk
Oma Falk - avatar
+ 1
maybe in your time line (feed)
7th Jul 2021, 7:21 PM
Oma Falk
Oma Falk - avatar
+ 1
5) Are you running Python in an IDE or in shell/ command line...?
7th Jul 2021, 7:24 PM
Lisa
Lisa - avatar
+ 1
Ahnaf Inan I agree with Oma Falk: It ain't silly, and we all run in problems like that from time to time. šŸ™‚ Thanks, you came back to this thread to share your solution!
8th Jul 2021, 5:13 PM
Lisa
Lisa - avatar
0
moment... u know it is pd.read_csv?
7th Jul 2021, 6:49 PM
Oma Falk
Oma Falk - avatar
0
ok... next try import pandas as pd df = pd.read.csv(rā€œC:\users\user\Desktop\Inanā€, encoding = "utf-8" ) print(df.head())
7th Jul 2021, 6:55 PM
Oma Falk
Oma Falk - avatar
0
did the encoding utf too And also the decode utf as well as engine=python
7th Jul 2021, 6:57 PM
Ahnaf Inan
Ahnaf Inan - avatar
0
@ Lisa sorry...I didnā€™t get it... How do I put the file name?
7th Jul 2021, 7:04 PM
Ahnaf Inan
Ahnaf Inan - avatar
0
Lisa I think Iā€™m dumb as heck. Anyways,I did it the way you said and it said file not found :(
7th Jul 2021, 7:17 PM
Ahnaf Inan
Ahnaf Inan - avatar
0
Just change the path of .csv file
8th Jul 2021, 2:20 PM
Naman Chhabra
Naman Chhabra - avatar