FileNotFoundError:使用Pandas

时间:2017-06-30 09:04:14

标签: python pandas

我举了一个例子,我收到了以下错误,并且不知道为什么。

# 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)

当我跑步时,我得到:

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

我认为这是正确的代码。

我从https://www.learnpython.org/en/Pandas_Basics获得了此代码 谢谢,我修好了

4 个答案:

答案 0 :(得分:1)

确保您的文件与python代码位于同一目录中,否则您需要为其指定目录路径。希望有用!

答案 1 :(得分:1)

这是因为你没有cars.csv个文件。打开文本编辑器,在同一目录中创建与.py文件相同的文件。

cars.csv:

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

现在尝试运行代码。你会得到输出,

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

pd.read_csv()所做的是读取csv文件(默认分隔符,,您也可以更改它)

答案 2 :(得分:0)

如果您使用此程序,则必须将程序和cars.csv保存在同一文件夹中。 cars = pd.read_csv('cars.csv')或者您可以像(r'C:\Users\Vikas Chauhan\Desktop\cars.csv')这样提供完整的csv文件路径。 你的代码是正确的。

import pandas as pd

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

# Print out cars
print(cars)

OutPut

  vikas   test
0  vika  test2

答案 3 :(得分:0)

数据集= pds.read_csv(os.path.join(os.getcwd(),“ Data.csv”))

使用getcwd获取当前目录。