我想用pd.read_csv(path,chunksize = N)打开一个csv文件,直到它的结尾以一种非常优雅和有效的方式。问题是,一旦指针不在文件中,就会发生以下错误消息:
df.get_chunk()
Traceback (most recent call last):
File "<ipython-input-115-061ea8dbcbad>", line 1, in <module>
df.get_chunk()
File "C:\Users\fedel\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 784, in get_chunk
return self.read(nrows=size)
File "C:\Users\fedel\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 763, in read
ret = self._engine.read(nrows)
File "C:\Users\fedel\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 1213, in read
data = self._reader.read(nrows)
File "pandas\parser.pyx", line 766, in pandas.parser.TextReader.read (pandas\parser.c:7988)
File "pandas\parser.pyx", line 813, in pandas.parser.TextReader._read_low_memory (pandas\parser.c:8629)
StopIteration
并且代码不能再继续了!
我相信try / except语句会避免我这条消息,因此代码会继续处理下一个问题。假设我有一个python DataFrame,就像你可以用以下代码行生成的那个
path = r"C:\Users\fedel\Desktop" + '\\fileName.csv'
pd.DataFrame( np.random.randn(30, 3), columns = list('abc')).to_csv(path, index = False)
df = pd.read_csv(path, chunksize = 6)
我认为像下面这样的声明可以避免该错误,让代码继续下一个问题
while True:
try:
df.get_chunk()
except TypeOfError:
funcyfunction()
你可以修复最后一个异常处理代码行吗?
答案 0 :(得分:1)
你可以尝试:
df = pd.read_csv(path, chunksize=6)
for chunk in df:
print(chunk)
如果你想在每个块内执行操作,你可以这样做:
for chunk in df:
chunk['d'] = chunk[['a', 'b']].mean(axis=1) # Average of columns 'a' and 'b'
print(chunk)