Python Pandas Error tokenizing data

后端 未结 30 2078
不知归路
不知归路 2020-11-22 04:49

I\'m trying to use pandas to manipulate a .csv file but I get this error:

pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 field

30条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:29

    It might be an issue with

    • the delimiters in your data
    • the first row, as @TomAugspurger noted

    To solve it, try specifying the sep and/or header arguments when calling read_csv. For instance,

    df = pandas.read_csv(fileName, sep='delimiter', header=None)
    

    In the code above, sep defines your delimiter and header=None tells pandas that your source data has no row for headers / column titles. Thus saith the docs: "If file contains no header row, then you should explicitly pass header=None". In this instance, pandas automatically creates whole-number indices for each field {0,1,2,...}.

    According to the docs, the delimiter thing should not be an issue. The docs say that "if sep is None [not specified], will try to automatically determine this." I however have not had good luck with this, including instances with obvious delimiters.

提交回复
热议问题