Cant read .csv file. EmptyDataError: No columns to parse from file

Question:

I am using a MAC laptop to read my .csv file but this error shows up:

EmptyDataError: No columns to parse from file.

Here is a look at my data file:
preview of the .csv file

I even checked whether the filepath of the file is correct and it turned out to be fine.

path = 'UserssyedwaqarHuma-IBM-MLhealthcare-dataset-stroke-data.csv' 
con = sq3.Connection(path)

I tried to define the path like this but it always gave the error:

path = 'Users/syedwaqar/Huma-IBM-ML/healthcare-dataset-stroke-data.csv'
con = sq3.Connection(path)

OperationalError: unable to open database file

After this: I tried to check if the filepath is correct, it shows that its correct. I wonder what the problem is.

Here is the main error after writing this line of code:

data = pd.read_csv(path)

————————————————————————— EmptyDataError Traceback (most recent call
last) in
—-> 1 data = pd.read_csv(path, header=None)

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.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, dialect, error_bad_lines,
warn_bad_lines, delim_whitespace, low_memory, memory_map,
float_precision)
684 )
685
–> 686 return _read(filepath_or_buffer, kwds)
687
688

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py in
_read(filepath_or_buffer, kwds)
450
451 # Create the parser.
–> 452 parser = TextFileReader(fp_or_buf, **kwds)
453
454 if chunksize or iterator:

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py in
init(self, f, engine, **kwds)
944 self.options["has_index_names"] = kwds["has_index_names"]
945
–> 946 self._make_engine(self.engine)
947
948 def close(self):

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py in
_make_engine(self, engine) 1176 def _make_engine(self, engine="c"): 1177 if engine == "c":
-> 1178 self._engine = CParserWrapper(self.f, **self.options) 1179 else: 1180 if engine == "python":

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py in
init(self, src, **kwds) 2006 kwds["usecols"] = self.usecols 2007
-> 2008 self._reader = parsers.TextReader(src, **kwds) 2009 self.unnamed_cols = self._reader.unnamed_cols 2010

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

EmptyDataError: No columns to parse from file

Kindly, help me fix this. I am unable to understand the issue.

Asked By: S HUMA SHAH

||

Answers:

You are probable using a wrong delimiter. This usually comes from you Mac OS Language & Region settings.

Take a look at this post you’ll get the information you need to fix this: https://harvestmedia.zendesk.com/hc/en-us/articles/360023978031-Opening-Excel-files-with-the-correct-CSV-list-separator

Answered By: iDev

I tried a lot of methods to solve this issue but to no avail. Finally, I found the solution on my own after scouring through a heap of info about reading a .csv file in pandas dataframe. I am posting the answer to my own question only to help those with the same issue.
There are a lot of reasons why your .csv file cant be read. One must check the preview of their file and look for all the arguments which need to be mentioned in the "pd.read_csv" function based on the preview of your file like: type of delimiter( tab-separated etc), blank header (in that case header= none). After checking for any required arguments which need to be put, if the issue persists. Then the issue might be with the file path. type in

pwd

This will print the working directory. and then you have to only specify the location after your working directory. e.g.
this shows how to specify the path of your file
Specify the path after the working directory. If your file is in the working directory then only mention the file name like I did.
But if your file is present in some other folder than you can either specify the succeeding folders after the working directory
e.g. your working directory is "/Users/username"
and your file is in a folder named ‘huma’ in ‘documents’ then you would write the below code:

path = 'Documents/huma/filename.csv'

or change the working directory to the folder where your file is present. use the below code:

cd /Users/Documents/huma/ 

The above line of code changed my working directory and now I have to only specify the file name:

path = 'filename.csv' 

you can check whether your file is present in the described path using this code:

os.path.isfile('filename.csv')
Answered By: S HUMA SHAH

I’ve gotten this when I am trying to read in a file that is not saved locally on my machine. For example, I use dropbox to save space on my local machine but if I try and read in a file that is saved in ‘the cloud’ I’ll hit this error. The only way I’ve found to solve this is to go to the file on your machine and save it locally first and the let your program read it in. The best solution would be to write something that will pull the file down first and then read it in, if that’s possible.

Answered By: David