TheZero
TheZero

Reputation: 270

FileNotFoundError: using Pandas

I ran an example, I get the following errors and don't know why.

# Import pandas as pd
import pandas as pd

# Import the cars.csv data: cars
cars = pd.read_csv('cars.csv')

# Print out cars
print(cars)

And when I run, I get:

Traceback (most recent call last):
  File "C:/Users/gaara_000/PycharmProjects/firstPj/index.py", line 2, in <module>
    cars = pd.read_csv('cars.csv')
  File "C:\Users\gaara_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\parsers.py", line 655, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\gaara_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\parsers.py", line 405, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\gaara_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\parsers.py", line 764, in __init__
    self._make_engine(self.engine)
  File "C:\Users\gaara_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\parsers.py", line 985, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\gaara_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\parsers.py", line 1605, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 394, in pandas._libs.parsers.TextReader.__cinit__ (pandas\_libs\parsers.c:4209)
  File "pandas\_libs\parsers.pyx", line 710, in pandas._libs.parsers.TextReader._setup_parser_source (pandas\_libs\parsers.c:8873)
FileNotFoundError: File b'cars.csv' does not exist

I think this is correct code.

I got this code from https://www.learnpython.org/en/Pandas_Basics

Upvotes: 3

Views: 20749

Answers (4)

Vipin Shukla
Vipin Shukla

Reputation: 233

dataset = pds.read_csv(os.path.join(os.getcwd(),"Data.csv"))

use getcwd to get current directory.

Upvotes: -1

user7592336
user7592336

Reputation:

You have to save both your program and cars.csv in same folder if you are using this. cars = pd.read_csv('cars.csv') or you can give full path to your csv file like this (r'C:\Users\Vikas Chauhan\Desktop\cars.csv'). Your code is correct.

import pandas as pd

cars = pd.read_csv(r'C:\Users\Vikas Chauhan\Desktop\cars.csv')

# Print out cars
print(cars)

OutPut is

  vikas   test
0  vika  test2

Upvotes: 4

void
void

Reputation: 2642

It's because you have no cars.csv file. Open a text editor and create the following file in the same directory as your .py file.

cars.csv:

CarName,Price
Bmw,50000$
Audi,20000$
Ferrari,100000$

Now try running the code. And you'll get the output,

   CarName    Price
0      Bmw   50000$
1     Audi   20000$
2  Ferrari  100000$

So what pd.read_csv() does is reads a csv file (default delimiter is , you can change that too)

Upvotes: 2

Hadi Farah
Hadi Farah

Reputation: 1162

Make sure your file is in the same Directory as your python code, Otherwise you need to give it a directory path. Hope that works!

Upvotes: 1

Related Questions