-1

I am working on a project in Jupyter and am trying to open a csv file into my project using pandas and it gives me this like of error

~\AppData\Local\Temp/ipykernel_32092/2226441776.py in <module>
----> 1 df = pd.read_csv("prices.csv")
      2 df

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

~\Documents\python\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 

~\Documents\python\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:

~\Documents\python\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):

~\Documents\python\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):

~\Documents\python\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 

~\Documents\python\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",

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

FileNotFoundError: [Errno 2] No such file or directory: 'prices.csv' 

how can I fix this as the lines of coding that I am using are :

df = pd.read_csv("prices.csv")
df

I also tried writing a code that got me into the path of where my file is using cd before using pandas but it also didn't work

  • Use the absolut path instead of the relative path. – JAdel Mar 15 '22 at 12:50
  • Does this answer your question? [How to open my files in data\_folder with pandas using relative path?](https://stackoverflow.com/questions/35384358/how-to-open-my-files-in-data-folder-with-pandas-using-relative-path) – JAdel Mar 15 '22 at 12:51
  • @JAdel I used the path and this is the error I got ` File "C:\Users\User\AppData\Local\Temp/ipykernel_32092/1693538409.py", line 1 df = pd.read_csv("C:\Users\User\Desktop\projects\Jupyter\Linear Regression\prices.csv.xlsx") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape ` – Sapphire Mar 15 '22 at 12:56
  • You can get the current working directory path by using: pwd() If the file that you are trying is not in this path, then it will obviously not find it. Try changing your path, or simply place the prices.csv in the current working directory – Kylian Mar 15 '22 at 13:10
  • use pathlib to manipulate the path will be better to avoid issue. see https://docs.python.org/3/library/pathlib.html you can also try replace "\" to "/" in your path string to see if it works – gftea Mar 15 '22 at 13:46

1 Answers1

0

I have actually found a solution to my own problem, I have tried all the suggested ones but still got an error, turns out that the matter was trivial, I just needed to change my excel file format from xlsx to csv.