+ 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 Respostas
+ 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