1

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 = 'Users\syedwaqar\Huma-IBM-ML\healthcare-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.

S HUMA SHAH
  • 31
  • 1
  • 1
  • 4
  • You could try it here, to see if the issue is with your setup or the file itself: https://colab.research.google.com/ – zabop Mar 08 '21 at 18:06
  • Might find this helpful: https://stackoverflow.com/questions/40193452/importing-text-file-no-columns-to-parse-from-file – zabop Mar 08 '21 at 18:06
  • A reproducible example would also be helpful: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – zabop Mar 08 '21 at 18:07

3 Answers3

2

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')
Dharman
  • 30,962
  • 25
  • 85
  • 135
S HUMA SHAH
  • 31
  • 1
  • 1
  • 4
0

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

iDev
  • 503
  • 5
  • 25
  • Thanks for your response, I tried the solution but the error persists. It shows the same error. Also, I have encountered another issue, after getting the same error for another method I deleted the file from the database, but it showed that the connection with the file path is intact and the file exists in the folder even though I deleted it. – S HUMA SHAH Mar 09 '21 at 06:07
  • kindly take a look. code: os.path.isfile('Users\syedwaqar\Huma-IBM-ML\Book2.csv') Output: True . this output is incorrect as I deleted the file. this is confusing me furthermore, I am not able to understand the real problem. Kindly, Help me understand it and solve the issue. – S HUMA SHAH Mar 09 '21 at 06:07
  • I misunderstood your problem at first. Sorry about that. – iDev Mar 09 '21 at 15:57
0

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.

David
  • 1
  • 1
  • Welcome to stackoverflow and thank you for the answer David! However, your answer does not provide any concrete solution. Also, the issue is not being caused due to file being placed on the server. Instead, the pandas is unable to find the file in the local system. You can find more information on how to write good answers at https://stackoverflow.com/help/how-to-answer – fam Oct 18 '22 at 12:41