Fix: pandas.errors.ParserError — Expected X Fields, Saw Y
pandas.errors.ParserError: Error tokenizing data. C error: Expected 8 fields in line 143, saw 9. This means one or more lines in your CSV don't have the same number of columns as the header. Here's the exact cause and tested fixes, no guessing.
What's actually happening
pandas' default C parser counts columns from the header row, then expects every subsequent row to split into exactly that many fields when it hits your delimiter. "Saw more fields than expected" almost always means a text value contains an unescaped delimiter, e.g. an address field 123 Main St, Apt 4 that was never wrapped in double quotes, so the comma inside it gets read as a new column boundary. "Saw fewer fields" usually means a row is truncated or a trailing delimiter is missing.
Step 1: find the exact bad rows first
Don't guess, locate every mismatched line before deciding how to handle it:
import csv
with open("file.csv", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
header = next(reader)
header_len = len(header)
for line_no, row in enumerate(reader, start=2):
if len(row) != header_len:
print(f"Line {line_no}: expected {header_len} fields, got {len(row)}")
print(row)Step 2: pick a fix
Option A, skip the bad rows (fast, loses data):
df = pd.read_csv("file.csv", on_bad_lines="skip", engine="python")Use on_bad_lines="warn" first to see what would be dropped before switching to "skip" in anything unattended.
Option B, let pandas' Python engine try to recover the row:
df = pd.read_csv("file.csv", engine="python", sep=None) # sniffs the real delimiterThe Python engine's delimiter sniffer (sep=None) handles inconsistent or unusual delimiters that the default C engine rejects outright, at the cost of slower parsing on large files.
Option C, fix the source file (correct, but manual):
Open the exact line numbers found in Step 1 and wrap any field containing the delimiter in double quotes, per RFC 4180. This is the only option that doesn't lose or guess at data.
Related errors you might be seeing instead
A few other pandas errors come from the same family of problems and get confused with this one:
UnicodeDecodeError: 'utf-8' codec can't decode byte...is an encoding mismatch, not a field-count problem, trypd.read_csv("file.csv", encoding="latin1")orencoding="windows-1252"if the file didn't come from a UTF-8 source.ParserErrorwith no field-count message, just "Error tokenizing data", usually means an unclosed quote rather than a missing delimiter, a text field that opens with"but never closes it swallows every following line into one giant field until the next stray quote.EmptyDataError: No columns to parse from filemeans the file is genuinely empty or the wrong file path was read, not a formatting issue at all.
Preventing this in code you write yourself
If you also control the code that generates the CSV, the more durable fix is upstream of pandas entirely. Python's own csv module and pandas' to_csv both support quoting every field unconditionally, not just when a delimiter happens to be detected in it:
import csv
df.to_csv("output.csv", index=False, quoting=csv.QUOTE_NONNUMERIC)This guarantees every text field is wrapped in quotes on write, so a comma or newline embedded in the data can never be misread as a structural delimiter by whoever reads the file next, including a future run of your own script.
If it's a one-off file rather than a script you're maintaining
Paste the file into our free CSV Lint tool first to get the exact line numbers with mismatched field counts, without running Python at all. Then either fix those lines by hand, or let Auto Fix repair ragged rows and encoding issues automatically in your browser.
Skip the debugging, free
Auto-repair ragged rows and encoding issues in your browser before you re-run pandas.
Fix My CSVTurn this into a saved workflow
Create a free account to save the steps from this guide as a reusable workflow and re-run it on any file, from any device.
Follow HowToCSV on Google
Add us as a preferred source on Google Search so our latest CSV guides and tutorials surface more often in your Top stories.