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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值