0
iris_df = pd.read_csv("iris.csv")

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    iris_df = pd.read_csv("iris.csv")
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 610, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 462, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 819, in __init__
    self._engine = self._make_engine(self.engine)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 1050, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 1867, in __init__
    self._open_handles(src, kwds)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 1362, in _open_handles
    self.handles = get_handle(
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\common.py", line 642, in get_handle
    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'iris.csv'

我应该怎么办?我将 pandas 导入为 pd 但它给了我错误。请帮助

4

1 回答 1

1

这可能是因为您没有将文件放在与代码文件完全相同的文件夹中。尝试将它们都保存在同一个文件夹中并尝试以下操作:

import pandas as pd
iris_df = pd.read_csv('./iris.csv')

或者您也可以尝试将确切路径复制到 iris.csv 文件,然后使用以下命令加载它:

iris_df = pd.read_csv(' #path to your iris.csv  file')

对于相对路径,./iris.csv告诉函数在与 iris.csv 文件的代码文件相同的文件夹中查找。但是,如果您的 iris.csv 文件位于另一个文件夹中,这将不起作用。在这种情况下:如果您在基于 Linux 的系统上,您的确切路径将类似于:

home/usr/Desktop/FolderName/iris.csv

对于 Windows 系统,绝对路径类似于:

C:/Desktop/FolderName/iris.csv
于 2021-04-08T11:44:22.307 回答