Python Pandas Error tokenizing data

后端 未结 30 2718
不知归路
不知归路 2020-11-22 04:49

I\'m trying to use pandas to manipulate a .csv file but I get this error:

pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 field

30条回答
  •  迷失自我
    2020-11-22 05:49

    An alternative that I have found to be useful in dealing with similar parsing errors uses the CSV module to re-route data into a pandas df. For example:

    import csv
    import pandas as pd
    path = 'C:/FileLocation/'
    file = 'filename.csv'
    f = open(path+file,'rt')
    reader = csv.reader(f)
    
    #once contents are available, I then put them in a list
    csv_list = []
    for l in reader:
        csv_list.append(l)
    f.close()
    #now pandas has no problem getting into a df
    df = pd.DataFrame(csv_list)
    

    I find the CSV module to be a bit more robust to poorly formatted comma separated files and so have had success with this route to address issues like these.

提交回复
热议问题