【问题标题】:Loading file into Jupyter Notebook将文件加载到 Jupyter Notebook
【发布时间】:2021-11-15 11:49:45
【问题描述】:

我尝试按照指示的方式将 csv 中的代码移动到与 Jupyter Notebook 所在的文件夹相同的文件夹中。它仍然没有读取它。我也在尝试将其转换为数据框并使其“描述”。我将在下面发布代码和错误。请帮忙!提前谢谢!

import pandas as pd
fish = pd.read_csv('c:\\Users\\M\anaconda3\\Scripts\\Fish')

fish2 = pd.DataFrame(fish)

fish2.to_csv('fishdata.csv')

fish2.describe()
OSError                                   Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11624/2289113011.py in <module>
----> 1 fish = pd.read_csv('c:\\Users\\M\anaconda3\\Scripts\\Fish')

~\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py in read_csv(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, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
    584     kwds.update(kwds_defaults)
    585 
--> 586     return _read(filepath_or_buffer, kwds)
    587 
    588 

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py in _read(filepath_or_buffer, kwds)
    480 
    481     # Create the parser.
--> 482     parser = TextFileReader(filepath_or_buffer, **kwds)
    483 
    484     if chunksize or iterator:

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py in __init__(self, f, engine, **kwds)
    809             self.options["has_index_names"] = kwds["has_index_names"]
    810 
--> 811         self._engine = self._make_engine(self.engine)
    812 
    813     def close(self):

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py in _make_engine(self, engine)
   1038             )
   1039         # error: Too many arguments for "ParserBase"
-> 1040         return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
   1041 
   1042     def _failover_to_python(self):

~\anaconda3\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py in __init__(self, src, **kwds)
     49 
     50         # open handles
---> 51         self._open_handles(src, kwds)
     52         assert self.handles is not None
     53 

~\anaconda3\lib\site-packages\pandas\io\parsers\base_parser.py in _open_handles(self, src, kwds)
    220         Let the readers open IOHandles after they are done with their potential raises.
    221         """
--> 222         self.handles = get_handle(
    223             src,
    224             "r",

~\anaconda3\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    699         if ioargs.encoding and "b" not in ioargs.mode:
    700             # Encoding
--> 701             handle = open(
    702                 handle,
    703                 ioargs.mode,

OSError: [Errno 22] Invalid argument: 'c:\\Users\\M\x07naconda3\\Scripts\\Fish'

【问题讨论】:

  • read_csv 函数实际上并没有从它的样子读取 csv 文件。您需要正确定义csv文件路径。

标签: python pandas dataframe jupyter-notebook


【解决方案1】:

您被明确告知read_csv 的参数无效。

OSError: [Errno 22] Invalid argument: 'c:\\Users\\M\x07naconda3\\Scripts\\Fish'

你好像漏掉了 cvs 文件,比如table.csv

您可以尝试使用os.getcwd()检查当前工作目录是否与csv文件相同。

【讨论】:

    【解决方案2】:

    您需要指定要读取的文件的扩展名。 例如:对于 csv 文件,您的代码可能如下所示

    fish = pd.read_csv('c:\\Users\\M\anaconda3\\Scripts\\Fish.csv')
    

    或者如果你的文件和你的代码在同一个目录下,你可以使用完整的文件名

    fish = pd.read_csv('Fish.csv')
    

    【讨论】:

      【解决方案3】:

      您可以尝试重命名文件,将.csv 附加到您的文件名

      【讨论】:

        猜你喜欢
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 2019-10-01
        • 2018-07-11
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 2016-06-17
        相关资源
        最近更新 更多