Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 Pandas Error: pandas.parser.CParserError: Error tokenizing data. C error: Expected 11 fields in line 5, saw 13

I checked out this answer as I am having a similar problem.

Python Pandas Error tokenizing data

However, for some reason ALL of my rows are being skipped.

My code is simple:

import pandas as pd

fname = "data.csv"
input_data = pd.read_csv(fname) 

and the error I get is:

  File "preprocessing.py", line 8, in <module>
    input_data = pd.read_csv(fname) #raw data file ---> pandas.core.frame.DataFrame type
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/io/parsers.py", line 465, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/io/parsers.py", line 251, in _read
    return parser.read()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/io/parsers.py", line 710, in read
    ret = self._engine.read(nrows)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/io/parsers.py", line 1154, in read
    data = self._reader.read(nrows)
  File "pandas/parser.pyx", line 754, in pandas.parser.TextReader.read (pandas/parser.c:7391)
  File "pandas/parser.pyx", line 776, in pandas.parser.TextReader._read_low_memory (pandas/parser.c:7631)
  File "pandas/parser.pyx", line 829, in pandas.parser.TextReader._read_rows (pandas/parser.c:8253)
  File "pandas/parser.pyx", line 816, in pandas.parser.TextReader._tokenize_rows (pandas/parser.c:8127)
  File "pandas/parser.pyx", line 1728, in pandas.parser.raise_parser_error (pandas/parser.c:20357)
pandas.parser.CParserError: Error tokenizing data. C error: Expected 11 fields in line 5, saw 13
like image 242
user1452494 Avatar asked Dec 02 '22 14:12

user1452494


2 Answers

Solution is to use pandas built-in delimiter "sniffing".

input_data = pd.read_csv(fname, sep=None) 
like image 59
user1452494 Avatar answered Dec 06 '22 11:12

user1452494


For those landing here, I got this error when the file was actually an .xls file not a true .csv. Try resaving as a csv in a spreadsheet app.

like image 45
Kate Stohr Avatar answered Dec 06 '22 11:12

Kate Stohr