FileNotFoundError:使用 Pandas

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44842656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:54:41  来源:igfitidea点击:

FileNotFoundError: using Pandas

pythonpandas

提问by Aki No Ko

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_BasicsThanks, i fixed that

我从https://www.learnpython.org/en/Pandas_Basics得到了这个代码 谢谢,我修复了

回答by Hadi Farah

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!

确保你的文件和你的python代码在同一个目录中,否则你需要给它一个目录路径。希望有效!

回答by void

It's because you have no cars.csvfile. Open a text editor and create the following file in the same directoryas your .pyfile.

那是因为你没有cars.csv文件。打开文本编辑器并在您的.py文件相同的目录中创建以下文件。

cars.csv:

汽车.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 csvfile (default delimiteris ,you can change that too)

那么什么pd.read_csv()是读取csv文件(默认分隔符,你也可以改变它)

回答by void

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.

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

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

回答by Vipin Shukla

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

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

use getcwd to get current directory.

使用 getcwd 获取当前目录。