Python Forum

Full Version: CSV import by Pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'
>>>
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.
Thank you but even when I use full path I get error
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