pythonpandas读取csv文件最后一行_使用numpy / pandas在Python中讀取CSV文件的最后N行

这篇博客对比了在Python中读取CSV文件最后N行的两种方法:完整解析后选择和仅解析最后N行。结果显示,使用`deque`配合`genfromtxt`的方法与`readlines`和切片操作耗时相当,对于大型文件,deque可能因减少内存消耗而更具优势。同时,通过`skip_header`读取文件末尾行的方法因为需要两次读取而较慢。
摘要由CSDN通过智能技术生成

7

With a small 10 line test file I tried 2 approaches - parse the whole thing and select the last N lines, versus load all lines, but only parse the last N:

使用一個小的10行測試文件,我嘗試了2種方法 - 解析整個事物並選擇最后N行,而不是加載所有行,但只解析最后N:

In [1025]: timeit np.genfromtxt('stack38704949.txt',delimiter=',')[-5:]

1000 loops, best of 3: 741 µs per loop

In [1026]: %%timeit

...: with open('stack38704949.txt','rb') as f:

...: lines = f.readlines()

...: np.genfromtxt(lines[-5:],delimiter=',')

1000 loops, best of 3: 378 µs per loop

This was tagged as a duplicate of Efficiently Read last 'n' rows of CSV into DataFrame. The accepted answer there used

這被標記為有效地將最后'n'行CSV讀入DataFrame的副本。那里接受的答案

from collections import deque

and collected the last N lines in that structure. It also u

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值