python pandas 抛出解析错误

标签 python pandas

我需要使用 python pandas 的帮助。我有一个包含这些数据的 A.txt (这是一个示例)

0003343 01901310 8193910 91931
9183131 89102010 7373819 83003
3692429 92920202

B.txt

424u20u 092u9j 902u39 9293u2
9u193jj 901u39 jdo910 903u98
ue9un88 

因此,对于 A.txt 我执行了以下操作,它确实打印了顶部列数的数据框

import pandas as pd

fileRead = pd.read_csv("A.txt", delim_whitespace = True, header=None, dtype=object)
print fileRead

结果

       0        1      2       3
0   0003343 01901310 8193910 91931
1   9183131 89102010 7373819 83003
2   3692429 92920202 NaN     NaN

但是如果我对 B.txt 执行相同的操作,则会出现错误

pandas.errors.ParserError: Too many columns specified: expected 4 and found 1

我不明白。应该是相同的结果。我应该怎么做才能解决这个问题? 感谢您的帮助和建议。

最佳答案

我认为你需要read_fwf :

df = pd.read_fwf('test3.txt', header=None, dtype=object)

print (df.head())
         0      1      2      3      4      5      6      7      8
0  0000000  00915  00517  00916  00517  00916  00517  00915  00517
1  0000010  00915  00518  00915  00518  00915  00517  00915  00517
2  0000020  00915  00518  00915  00517  00915  00516  00915  00517
3  0000030  00915  00517  00915  00517  00915  00517  00915  00517
4  0000040  00915  00517  00916  00517  00915  00517  00915  00517

print (df.tail())
              0      1      2      3      4      5      6      7      8
262140  03fffc0  00916  00513  00916  00514  00916  00516  00916  00514
262141  03fffd0  00916  00513  00916  00514  00916  00514  00916  00514
262142  03fffe0  00916  00514  00916  00514  00917  00514  00916  00514
262143  03ffff0  00916  00514  00916  00514  00916  00514  00916  00514
262144  0400000    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN

编辑:

作为 EDchum pointed最后一行可能被删除。它似乎在 python 3.6 中。它会默默地删除。

关于python pandas 抛出解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184775/

相关文章:

python - 产品代码看起来像abcd2343,如何按字母和数字分割?

javascript - 使用scrapy抓取包含 anchor 标记<a href = "#">的网页

Pandas 数据框到二维 numpy 数组

python - 每日 cron 运行终止,因为它超过了最大运行时间

python - ValueError : not enough values to unpack (expected 8, 得到 7) - 忽略不满足此要求的文件 - python

python - 运行测试时 Django 1.5.1 'ImportError: No module named urls'

python - 将多行数据转换为单列数据

python - 将顺序与列中的项目合并

python - 将 pandas 中的系列分组

pandas - Python - reshape 、旋转、unstack - 多重索引