我试图使用熊猫操作.csv文件,但我得到这个错误:
pandas.parser.CParserError:标记数据错误。C错误:第3行有2个字段,见12
我试着读过熊猫的文件,但一无所获。
我的代码很简单:
path = 'GOOG Key Ratios.csv'
#print(open(path).read())
data = pd.read_csv(path)
我该如何解决这个问题?我应该使用csv模块还是其他语言?
文件来自晨星公司
我试图使用熊猫操作.csv文件,但我得到这个错误:
pandas.parser.CParserError:标记数据错误。C错误:第3行有2个字段,见12
我试着读过熊猫的文件,但一无所获。
我的代码很简单:
path = 'GOOG Key Ratios.csv'
#print(open(path).read())
data = pd.read_csv(path)
我该如何解决这个问题?我应该使用csv模块还是其他语言?
文件来自晨星公司
当前回答
对我来说,问题是一个新列被附加到我的CSV盘中。如果我使用error_bad_lines=False,接受的答案解决方案将不起作用,因为未来的每一行都将被丢弃。
这种情况下的解决方案是使用pd.read_csv()中的usecols参数。通过这种方式,我可以只指定需要读入CSV中的列,并且只要标题列存在(并且列名不改变),我的Python代码将对未来的CSV更改保持弹性。
usecols : list-like or callable, optional Return a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order.
例子
my_columns = ['foo', 'bar', 'bob']
df = pd.read_csv(file_path, usecols=my_columns)
这样做的另一个好处是,如果我只使用一个有18-20列的CSV中的3-4列,我可以将更少的数据加载到内存中。
其他回答
我有同样的问题,当read_csv: ParserError:错误标记数据。 我只是把旧的csv文件保存为一个新的csv文件。问题解决了!
这就是我所做的。
Sep ='::'解决了我的问题:
data=pd.read_csv('C:\\Users\\HP\\Downloads\\NPL ASSINGMENT 2 imdb_labelled\\imdb_labelled.txt',engine='python',header=None,sep='::')
这可能是个问题
数据中的分隔符 第一行,正如@TomAugspurger所指出的
要解决这个问题,请在调用read_csv时尝试指定sep和/或头参数。例如,
df = pandas.read_csv(filepath, sep='delimiter', header=None)
在上面的代码中,sep定义了您的分隔符和header=None,告诉pandas您的源数据没有作为标题/列标题的行。因此,文档说:“如果文件不包含标题行,那么你应该显式地传递header=None”。在这种情况下,pandas会自动为每个字段{0,1,2,…}创建整数索引。
根据文档,分隔符应该不是问题。文档中说“如果sep为None[未指定],将尝试自动确定此值。”然而,我在这方面运气不太好,包括带有明显分隔符的实例。
另一种解决方案可能是尝试自动检测分隔符
# use the first 2 lines of the file to detect separator
temp_lines = csv_file.readline() + '\n' + csv_file.readline()
dialect = csv.Sniffer().sniff(temp_lines, delimiters=';,')
# remember to go back to the start of the file for the next time it's read
csv_file.seek(0)
df = pd.read_csv(csv_file, sep=dialect.delimiter)
我遇到了这个问题,我试图在不传递列名的情况下读取CSV。
df = pd.read_csv(filename, header=None)
我事先在一个列表中指定了列名,然后将它们传递到名称中,它立即解决了这个问题。如果您没有设置列名,您可以创建与数据中可能存在的最大列数量一样多的占位符名称。
col_names = ["col1", "col2", "col3", ...]
df = pd.read_csv(filename, names=col_names)
在处理类似的解析错误时,我发现另一种方法很有用,它使用CSV模块将数据重新路由到pandas df。例如:
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)
我发现CSV模块对于格式不佳的逗号分隔的文件更加健壮,因此已经成功地用这种方法解决了诸如此类的问题。