【问题标题】:Count of lines from Pandas read_csv returning less than expectedPandas read_csv 返回的行数少于预期
【发布时间】:2021-06-01 06:22:22
【问题描述】:

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)

【问题讨论】:

  • 默认情况下,skip_blank_lines 参数为 True,对应于 read_csv。这将“跳过空白行而不是解释为 NaN 值。”
  • 嗨@HenryEcker,我的文件的第一列总是被填充。无论如何,我已经尝试过 skip_blank_lines=False 并得到了相同的结果 (19929388)。
  • 使用 engine='python' 我遇到了一些错误,例如 Skipping line 182405: '|'预计在 '"' 之后。我认为这没有意义,因为我没有在 read_csv 上将引用作为参数传递

标签: python pandas


【解决方案1】:

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

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

这篇文章帮助了我link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多