Skip to main content
 首页 » 编程设计

python之Pandas .errors.ParserError : Too many columns specified: expected 9996 and found 9808

2025年05月04日23itcoder

当我使用 pandas 处理我的数据时,出现标题这样的错误。我的数据列不相等,所以我按降序排列。第一行最长,下一行较短,依此类推。当文件较小时,pandas 可以成功处理。但是在我将所有数据写入文件后,它无法处理并显示此错误。

这是我的代码:

def sequencein(filepath):     
    print (filepath)     
    print("time", time.time())     
    data = pd.read_table(filepath, header=None)     
    print("time", time.time())     
    matr = data.values     
    print("sequence shape:", matr.shape)     
    return matr 

文件的行尾如下所示: enter image description here

请您参考如下方法:

documentation说有两个引擎:

engine : {‘c’, ‘python’}, optional

Parser engine to use. The C engine is faster while the python engine is currently more feature-complete.

问题似乎只出现在“c”引擎上,它是为较大的文件自动选择的。

所以,你可以试试

data = pd.read_table(filepath, header=None, engine='python')