【问题标题】:Trying to read file_url of .csv file in Padas returning file not found试图在 Pandas 中读取 .csv 文件的文件 url,返回文件未找到
【发布时间】:2022-01-05 04:16:22
【问题描述】:

这是我上课期间的练习,我无法参加,所以我私下赶上。 我正在尝试使用 Pandas 读取我的 .csv 文件。我正在使用 Jupiter,并在我的 Jupiter 中正确上传了 .csv 文件。 但由于某种原因,我的代码无法读取我的文件。

你能知道为什么吗? 谢谢! ** 示例数据:HR 员工流失率和绩效 ** 您可以从此处获取: 我已经从以下 url 下载了这个文件,并将它上传到我的 Jupiter。 https://www.kaggle.com/patelprashant/employee-attrition#WA_Fn-UseC_-HR-Employee-Attrition.csv

file_url = "HR_Employee_Attrition.csv"

data = pd.read_csv(file_url)

data.head()##->同data.head(5)

<ipython-input-19-201da5b1caca> in <module>
----> 1 data = pd.read_csv(file_url)
      2 # here the rows are being indexed by one of the columns ->  'EmployeeNumber'
      3 # all other column headings of the csv file would become columns.
      4 
      5 data.head()  ## same as data.head(5)

/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    674         )
    675 
--> 676         return _read(filepath_or_buffer, kwds)
    677 
    678     parser_f.__name__ = name

/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    446 
    447     # Create the parser.
--> 448     parser = TextFileReader(fp_or_buf, **kwds)
    449 
    450     if chunksize or iterator:

/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    878             self.options["has_index_names"] = kwds["has_index_names"]
    879 
--> 880         self._make_engine(self.engine)
    881 
    882     def close(self):

/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
   1112     def _make_engine(self, engine="c"):
   1113         if engine == "c":
-> 1114             self._engine = CParserWrapper(self.f, **self.options)
   1115         else:
   1116             if engine == "python":

/usr/local/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   1889         kwds["usecols"] = self.usecols
   1890 
-> 1891         self._reader = parsers.TextReader(src, **kwds)
   1892         self.unnamed_cols = self._reader.unnamed_cols
   1893 

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File HR_Employee_Attrition.csv does not exist: 'HR_Employee_Attrition.csv'```

【问题讨论】:

  • 您是否已将 CSV 文件上传到与笔记本相同的文件夹中?如果否,请指定 CSV 文件的完整路径。

标签: python-3.x pandas csv file


【解决方案1】:

我认为 pd.read 函数 csv 参数中 csv 的位置不正确。所以检查csv文件的路径(在参数中提供完整的路径位置字符串)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多