Python Pandas错误标记数据:如何避免因长度不同而导致的错误

我正在尝试使用pandas read_csv函数读取* .dat文件。

df = pd.read_csv(file,skiprows=0,header=None,sep=" ",parse_dates=[[0,1]])

数据如下:

2019-06-01 04:00:22 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:00:32 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:00:42 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:00:52 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:01:02 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:01:12 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:01:22 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:01:32 PW  100  2000  2000 /// // // // ////// ////// ////

我收到标记错误:

ParserError: Error tokenizing data. C error: Expected 16 fields in line 242,saw 17

我认为是造成此错误的原因,因为第242行中的第6列中的值比之前的行中的低,例如列6保持为2000或具有4位数字的值(例如1501),但在第242行中降至991(三位数字)。

2019-06-01 04:39:32 PW  100  2000  2000 /// // // // ////// ////// ////
2019-06-01 04:39:42 PW  100  1501  2000 /// // // // ////// ////// ////
2019-06-01 04:39:52 PW  100  1501  2000 /// // // // ////// ////// ////
2019-06-01 04:40:02 PW  100  1501  2000 /// // // // ////// ////// ////
2019-06-01 04:40:12 PW  100  1187  2000 /// // // // ////// ////// ////
2019-06-01 04:40:22 PW  100  1187  2000 /// // // // ////// ////// ////
2019-06-01 04:40:32 PW  100   991  2000 /// // // // ////// ////// ////

如何摆脱这个错误?

error_bad_lines = False不能选择,因为我需要这些值

mgdbrrr 回答:Python Pandas错误标记数据:如何避免因长度不同而导致的错误

您应该使用NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="select2-chosen1"]"} (Session info: chrome=78.0.3904.108) sep=" +"而不是sep="\s+"。对于后者,将多个空白分隔为多个空列,这将在空白数目更改时导致错误。

或者,您可以指定sep=" "而不是delim_whitespace=Truesep

本文链接:https://www.f2er.com/2978805.html

大家都在问