首页 > 解决方案 > Pandas read_csv 返回的行数少于预期

问题描述

Pandas read_csv shape[0] 或 len(index) 函数返回我计算机上的 2000 万行文件的 19929388 行。但是下面的powershell命令返回2000万。我的代码有问题吗?

Powershell - 返回 20000000

[int]$LinesInFile = 0
$reader = New-Object IO.StreamReader 'File.csv'
 while($reader.ReadLine() -ne $null){ $LinesInFile++ }

Python - 返回 19929388

df = pd.read_csv(path, low_memory=(False), delimiter='|', header=None)    
index = df.shape[0]
print(index)

标签: pythonpandas

解决方案


弄清楚了。由于未闭合的双引号,Pandas 跳过了一些行,即使我没有为我的文件声明限定符。

只需在 pd.read_csv() 上添加quoting=csv.QUOTE_NONE即可解决。

这篇文章帮助我找到了链接


推荐阅读