. Advertisement .
..3..
. Advertisement .
..4..
A common error message that people running a Python program will receive is the error message `pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12`. This error usually occurs when a dataset is being read but has missing data. This blog will go through some steps that you can take to correct this issue.
When Does The Error “pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12” Happen?
When attempting to read a csv file, you may encounter the following error.
pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12
Here is the code you are attempting to run.
StudentData = 'stdseven.csv'
#print("data read start")
data = pd.read_csv(StudentData)
This error could be caused by the existence of bad lines in your csv file.
How To Fix The Error “pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12”?
Option 1: Dismiss bad lines
All you have to do now is to ignore the bad lines. Simply set the value of error bad lines to False.
data = pd.read_csv('yourfile.csv', error_bad_lines=False)
Bad lines will be skipped as a result of this. Now, you have fixed your issue.
Option 2: Clarify the sep
Simply use the separator /t to specify the Sep using the tab character (t). So, try opening the file with the following code:
data=pd.read_csv("yourfile.csv", sep='\t')
Your problem should now be resolved.
Option 3: Define the sep and/or header arguments
Excepting two solutions mentioned above, you can define the sep and/or header arguments when you call read_csv to fix the error “pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12”. Take a look at the program below to further understand this method:
pandas.read_csv(fileName, sep='you_delimiter', header=None)
The sep option indicates your delimiter (for instance, “t” in the code above), and header=None informs pandas that your source data has no rows for headers or column titles.
This method is very efficiently. It will helps you resolve your error and make your program run well without any errors. Let’s use it to get your desired results.
Conclusion
We hope you will enjoy our article on how to fix the error “pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12”. If you have any additional questions or comments, please leave them in the comment section below. Thank you for reading; we are always excited when one of our articles is able to provide useful information on a topic like this!
Read more
→ Tips On Solving The Error: “runtimeError: package fails to pass a sanity check for numpy and pandas”
Leave a comment