【发布时间】:2016-05-25 14:22:18
【问题描述】:
我正在使用以下代码连接我从here 下载的几个文件(候选主文件);但也可以在这里找到:
https://github.com/108michael/ms_thesis/blob/master/cn06.txt
https://github.com/108michael/ms_thesis/blob/master/cn08.txt
https://github.com/108michael/ms_thesis/blob/master/cn10.txt
https://github.com/108michael/ms_thesis/blob/master/cn12.txt
https://github.com/108michael/ms_thesis/blob/master/cn14.txt
import numpy as np
import pandas as pd
import glob
df = pd.concat((pd.read_csv(f, header=None, names=['feccandid','candname',\
'party','date', 'state', 'chamber', 'district', 'incumb.challeng', \
'cand_status', '1', '2','3','4', '5', '6' ], usecols=['feccandid', \
'party', 'date', 'state', 'chamber'])for f in glob.glob\
('/home/jayaramdas/anaconda3/Thesis/FEC/cn_data/cn**.txt')))
我收到以下错误:
CParserError: Error tokenizing data. C error: Expected 2 fields in line 58, saw 4
有人知道吗?
【问题讨论】:
-
当您在一个文件上使用
read_csv时,数据框看起来是否符合预期?您可能需要将delimiter = "|"传递给read_csv函数。 -
我刚刚尝试只读取一个文件并使用 ?
sep='|'; and then after your comment ^ I tried usingdelimiter = '|'` 并且效果很好。我再次尝试了整个操作并解决了问题!谢谢你的线索!° -
很高兴它成功了!我添加它作为答案,以防其他人有同样的问题。
标签: python pandas concatenation glob