简体   繁体   English

如何修复 ParserError:错误标记数据 CSV pandas

[英]How to fix ParserError: Error tokenizing data for CSV pandas

Sample DF(1 column without header name):样本 DF(1 列没有 header 名称):

vers    2.1.0  
info    days    6
info    x       a
info    y       b 

Here is my code and error message:这是我的代码和错误消息:

df = pd.read_csv("64881_info.csv")
ParserError: Error tokenizing data. C error

I tried to fix:我试图修复:

import pandas as pd
df = pd.read_csv("64881_info.csv", error_bad_lines=False)

It works but column header name shift right by next.它有效,但列 header 名称向右移动。 Row indexing is not showing also after reading the csv file.读取 csv 文件后,行索引也未显示。 How can I fix this?我怎样才能解决这个问题?

Output df: Output DF:

        vers    2.1.0
info    days    6     
info    x       a
info    y       b

After resetting row indexing, it works!重置行索引后,它起作用了!

df = df.reset_index()

output: output:

    index   vers    2.1.0
0   info    days    6
1   info    x       a
2   info    y       b

You could try to just skip the bad lines if you don't think there are too many:如果你不认为有太多,你可以尝试跳过错误的行:

df= pd.read_csv('64881_info.csv', on_bad_lines='skip') df= pd.read_csv('64881_info.csv', on_bad_lines='skip')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM