I am trying to read a csv
file using pandas
df1 = pd.read_csv('panda_error.csv', header=None, sep=',')
But I am getting this error:
ParserError: Error tokenizing data. C error: Expected 7 fields in line 4, saw 10
For reproducibility, here is the csv file panda_error.csv
superkingdom:Bacteria , phylum:Actinobacteria , class:Actinobacteria , order:Corynebacteriales , family:Corynebacteriaceae , genus:Corynebacterium , species:Corynebacterium efficiens 1
superkingdom:Bacteria , phylum:Proteobacteria , class:Alphaproteobacteria , order:Rhizobiales , family:Aurantimonadaceae , genus:Aurantimonas , species:Aurantimonas manganoxydans 1
superkingdom:Bacteria , phylum:Proteobacteria , subphylum:delta/epsilon subdivisions , class:Deltaproteobacteria , no rank:unclassified Deltaproteobacteria , genus:Candidatus Entotheonella 1
superkingdom:Bacteria , phylum:Proteobacteria , class:Gammaproteobacteria , order:Pseudomonadales , family:Pseudomonadaceae , genus:Pseudomonas , species group:Pseudomonas syringae group , species subgroup:Pseudomonas syringae group genomosp. 2 , species:Pseudomonas amygdali , no rank:Pseudomonas amygdali pv. tabaci 1
superkingdom:Bacteria , phylum:Actinobacteria , class:Actinobacteria , order:Corynebacteriales , family:Nocardiaceae , genus:Rhodococcus , species:Rhodococcus wratislaviensis 1
superkingdom:Bacteria , phylum:Firmicutes , class:Clostridia , order:Clostridiales , family:Peptostreptococcaceae , genus:Peptoclostridium , species:Peptoclostridium difficile1
I am not really sure why this is happening and how to resolve this. The other answers just suggest to 1. ignore the troubling lines using error_bad_lines=False
which I do not want to do, or 2. specific to certain scenario.
Here is the full error message if this helps:
---------------------------------------------------------------------------
ParserError Traceback (most recent call last)
<ipython-input-34-72c0ecaf0513> in <module>
----> 1 df1 = pd.read_csv('panda_error.csv', header=None, sep=',')
/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(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)
683 )
684
--> 685 return _read(filepath_or_buffer, kwds)
686
687 parser_f.__name__ = name
/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
461
462 try:
--> 463 data = parser.read(nrows)
464 finally:
465 parser.close()
/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in read(self, nrows)
1152 def read(self, nrows=None):
1153 nrows = _validate_integer("nrows", nrows)
-> 1154 ret = self._engine.read(nrows)
1155
1156 # May alter columns / col_dict
/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in read(self, nrows)
2057 def read(self, nrows=None):
2058 try:
-> 2059 data = self._reader.read(nrows)
2060 except StopIteration:
2061 if self._first_chunk:
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()
pandas/_libs/parsers.pyx in pandas._libs.parsers.raise_parser_error()
ParserError: Error tokenizing data. C error: Expected 7 fields in line 4, saw 10