Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV import by Pandas
#1
I want to import a csv file by pd.read_csv and I put the file on site-pakages but python gives the error that the file doesn't exist, would uou please help me? The code and error are below:


> a=pd.read_csv('sample')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
a=pd.read_csv('sample')
File "C:\Users\maryam\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\parsers.py", line 676, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\maryam\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\parsers.py", line 448, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "C:\Users\maryam\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\parsers.py", line 880, in __init__
self._make_engine(self.engine)
File "C:\Users\maryam\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\parsers.py", line 1114, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\maryam\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\parsers.py", line 1891, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 374, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 674, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File sample does not exist: 'sample'
>>>
Reply
#2
Output:
FileNotFoundError: [Errno 2] File sample does not exist: 'sample'
The file does not exist in the working directory. Use the full path to the file.
Reply
#3
Thank you but even when I use full path I get error
Reply
#4
Remember on Windows is normal to use file name extensions on most files.
So just sample is very uncommon,when sample.csv is not.
# read_csv.py
import pandas as pd

df = pd.read_csv('sample.csv')
print(df)
So if read_csv.py and sample.csv is in same folder no Path is needed.

Here use path to .csv file is placed.
# read_csv.py
import pandas as pd

df = pd.read_csv(r'C:\code\sample.csv')
print(df)
Both yield no error and read the .csv file.
Output:
Name Code 0 Afghanistan AF 1 Ă…land Islands AX 2 Albania AL 3 Algeria DZ 4 American Samoa AS 5 Andorra AD
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 724 Aug-06-2023, 01:09 AM
Last Post: aupres
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 853 May-29-2023, 02:42 PM
Last Post: Nietzsche
  Illegal instruction (core dumped) when import pandas. amandacstr 1 2,114 Dec-31-2022, 11:31 PM
Last Post: stevendaprano
  help on pandas circular import jip31 2 8,384 May-18-2021, 11:54 AM
Last Post: jip31
  Can't import pandas j_cooper9 1 2,013 Aug-02-2020, 08:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020