14

I am currently learning Pandas for data analysis and having some issues reading a csv file in Atom editor.

When I am running the following code:

import pandas as pd 

df = pd.read_csv("FBI-CRIME11.csv")

print(df.head())

I get an error message, which ends with

OSError: File b'FBI-CRIME11.csv' does not exist

Here is the directory to the file: /Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv".

When i try to run it this way:

df = pd.read_csv(Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv")

I get another error:

NameError: name 'Users' is not defined

I have also put this directory into the "Project Home" field in the editor settings, though I am not quite sure if it makes any difference.

I bet there is an easy way to get it to work. I would really appreciate your help!

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Aleksei Nabatov
  • 151
  • 1
  • 1
  • 5

17 Answers17

19

Have you tried?

df = pd.read_csv("Users/alekseinabatov/Documents/Python/FBI-CRIME11.csv")

or maybe

df = pd.read_csv('Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv"')

(If the file name has quotes)

arutaku
  • 5,937
  • 1
  • 24
  • 38
  • The first suggestion should work if you tweak it to include the reference to the root directory, i.e.: `pd.read_csv("/Users/alekseinabatov/Documents/Python/FBI-CRIME11.csv")`. The file name should not have quotation symbols in it. – Frangipanes Sep 01 '16 at 09:26
  • the following code worked: pd.read_csv("/Users/alekseinabatov/Documents/Python/FBI-CRIM‌​E11.csv"). Thank you so much! – Aleksei Nabatov Sep 01 '16 at 09:45
  • Any idea how i can make this a default directory for the future projects? – Aleksei Nabatov Sep 01 '16 at 09:48
  • @AlekseiNabatov A pythonic way is declare a constant containing the path: `PATH = 'Users/alekseinabatov/Documents/Python/'` and then `pd.read_csv(PATH + 'FBI-CRIME11.csv')`. If this path will be shared across projects, you can use an environment variable containing the path ;-) – arutaku Sep 02 '16 at 09:01
  • in my case changed ev = pd.read_csv(join(sub,'evaluation.csv')) -> ev = pd.read_csv(join(sub,"evaluation.csv")) – Jumabek Alikhanov Nov 06 '19 at 03:56
5

Just referring to the filename like

df = pd.read_csv("FBI-CRIME11.csv")

generally only works if the file is in the same directory as the script.

If you are using windows, make sure you specify the path to the file as follows:

PATH = "C:\\Users\\path\\to\\file.csv"
BartDur
  • 1,086
  • 1
  • 12
  • 21
  • 1
    Using Mac actually. I was wondering how to set this directory as default one so I do not have to write the whole path every time I do input/output operations. – Aleksei Nabatov Sep 01 '16 at 10:06
3

Had an issue with the path, it turns out that you need to specify the first '/' to get it to work! I am using VSCode/Python on macOS

Adnane
  • 39
  • 1
3

I also experienced the same problem I solved as follows:

dataset = pd.read_csv('C:\\Users\\path\\to\\file.csv')
mx0
  • 6,445
  • 12
  • 49
  • 54
2

Being on jupyter notebook it works for me including the relative path only. For example:

df = pd.read_csv ('file.csv')

But, for example, in vscode I have to put the complete path:

df = pd.read_csv ('/home/code/file.csv')
1

You are missing '/' before Users. I assume that you are using a MAC guessing from the file path names. You root directory is '/'.

1

I had the same issue, but it was happening because my file was called "geo_data.csv.csv" - new laptop wasn't showing file extensions, so the name issue was invisible in Windows Explorer. Very silly, I know, but if this solution doesn't work for you, try that :-)

George
  • 13
  • 3
1

Just change the CSV file name. Once I changed it for me, it worked fine. Previously I gave data.csv then I changed it to CNC_1.csv.

zappee
  • 20,148
  • 14
  • 73
  • 129
1

What worked for me:

import csv
import pandas as pd
import os

base =os.path.normpath(r"path")



with open(base, 'r') as csvfile:
    readCSV = csv.reader(csvfile, delimiter='|')
    data=[]
    for row in readCSV:
        data.append(row)
    df = pd.DataFrame(data[1:],columns=data[0][0:15])
    print(df)


This reads in the file , delimit by |, and appends to list which is converted to a pandas df (taking 15 columns)
0

Make sure your source file is saved in .csv format. I tried all the steps of adding the full path to the file, including and deleting the header=0, adding skiprows=0 but nothing works as I saved the excel file(data file) in workbook format and not in CSV format. so keep in mind to first check your file extension.

monica
  • 1
  • 2
0

Adnane's answer helped me.

Here's my full code on mac, hope this helps someone. All my csv files are saved in /Users/lionelyu/Documents/Python/Python Projects/

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')

path = '/Users/lionelyu/Documents/Python/Python Projects/'

aapl = pd.read_csv(path + 'AAPL_CLOSE.csv',index_col='Date',parse_dates=True)
cisco = pd.read_csv(path + 'CISCO_CLOSE.csv',index_col='Date',parse_dates=True)
ibm = pd.read_csv(path + 'IBM_CLOSE.csv',index_col='Date',parse_dates=True)
amzn = pd.read_csv(path + 'AMZN_CLOSE.csv',index_col='Date',parse_dates=True)
Lionel Yu
  • 328
  • 3
  • 11
0

Run "pwd" command first in cli to find out what is your current project's direction and then add the name of the file to your path!

DragoRoff
  • 55
  • 4
0

Try this

import os 
cd = os.getcwd()
dataset_train = pd.read_csv(cd+"/Google_Stock_Price_Train.csv")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Brett Young
  • 168
  • 2
  • 8
0

In my case I just removed .csv from the end. I am using ubuntu.

pd.read_csv("/home/mypc/Documents/pcap/s2csv")
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Kunal C
  • 1
  • 3
0

Sometimes we ignore a little bit issue which is not a Python or IDE fault its logical error We assumed a file .csv which is not a .csv file its a Excell Worksheet file have a look


Tiktok file which is worksheet file not a CSV file Shown in Green border


When you try to open that file using Import compiler will through the error have a look enter image description here

To Resolve the issue

open your Target file into Microsoft Excell and save that file in .csv format it is important to note that Encoding is important because it will help you to open the file when you try to open it with

with open('YourTargetFile.csv','r',encoding='UTF-8') as file:

enter image description here

So you are set to go now Try to open your file as this

import csv
with open('plain.csv','r',encoding='UTF-8') as file:
load = csv.reader(file)
for line in load:
    print(line)

Here is the Output Plain.csv File Running

-1

What works for me is

dataset = pd.read_csv('FBI_CRIME11.csv')

Highlight it and press enter. It also depends on the IDE you are using. I am using Anaconda Spyder or Jupiter.

B--rian
  • 5,578
  • 10
  • 38
  • 89
tshele litabe
  • 126
  • 1
  • 3
-2

I am using a Mac. I had the same problem wherein .csv file was in the same folder where the python script was placed, however, Spyder still was unable to locate the file. I changed the file name from capital letters to all small letters and it worked.