将csv导入pandas数据帧时不读取所有行

时间:2015-10-16 02:50:23

标签: python-3.x csv pandas machine-learning kaggle

我正在尝试kaggle挑战here,不幸的是我陷入了一个非常基本的步骤。我有限的python知识必须归咎于此。 我试图通过执行以下命令将datasets读入pandas数据帧:

test = pd.DataFrame.from_csv("C:/Name/DataMining/hillary/data/output/emails.csv")

问题是你发现的这个文件有超过300,000条记录,但我只阅读7945,21。

print (test.shape)
(7945, 21)

现在我已经仔细检查了文件,我找不到有关第7945行的任何特殊信息。任何指示为什么会发生这种情况。似乎非常普通的情况,我希望有些遇到过这个错误的人可以帮助我。

1 个答案:

答案 0 :(得分:5)

我认为更好的是使用带有参数quoting=csv.QUOTE_NONEerror_bad_lines=False的函数read_csvlink

import pandas as pd
import csv

test = pd.read_csv("output/Emails.csv", quoting=csv.QUOTE_NONE, error_bad_lines=False)

print (test.shape)
#(381422, 22)

但是会跳过一些数据(有问题)。

如果您想要跳过电子邮件正文数据,可以使用:

import pandas as pd
import csv

test = pd.read_csv("output/Emails.csv", quoting=csv.QUOTE_NONE,  sep=',', error_bad_lines=False, header=None,
    names=["Id","DocNumber","MetadataSubject","MetadataTo","MetadataFrom","SenderPersonId","MetadataDateSent","MetadataDateReleased","MetadataPdfLink","MetadataCaseNumber","MetadataDocumentClass","ExtractedSubject","ExtractedTo","ExtractedFrom","ExtractedCc","ExtractedDateSent","ExtractedCaseNumber","ExtractedDocNumber","ExtractedDateReleased","ExtractedReleaseInPartOrFull","ExtractedBodyText","RawText"])

print (test.shape)

#delete row with NaN in column MetadataFrom
test = test.dropna(subset=['MetadataFrom'])
#delete headers in data
test = test[test.MetadataFrom != 'MetadataFrom']