V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sdushn
0D
V2EX  ›  Python

迫于不会,请教大佬们如何用 pandas 分片读取 csv 文件

  •  
  •   sdushn · Feb 7, 2021 · 1474 views
    This topic created in 1908 days ago, the information mentioned may be changed or developed.

    背景

    我这里有一个被分片存储的 csv 文件,如下所示 | time| A | B | C | D| | ---- | -- | - | -- | -- | | 20:00|0| 1| 2| 3| | time| E | F | G | H| | 20:00|4|5|6|7|| ... 现在想读成 | time| A | B | C | D| E | F | G | H| | --- | --- | --- | --- | --- | --- | --- | --- | --- | | 20:00|0|1|2|3|4|5|6|7|

    sdushn
        1
    sdushn  
    OP
       Feb 7, 2021
    ### 背景
    使用 pandas 库处理一个性能参数的 csv 文件,但是 csv 文件是分片的,格式如下

    | time | A | B | C | D |
    | ---- |---|---|---|---|
    | 20:00| 0 | 1 | 2 | 3 |
    | time | E | F | G | H |
    | 20:00| 4 | 5 | 6 | 7 |
    |...|

    现在想读成如下格式,存成一个 dataframe 便于下游继续处理

    | time | A | B | C | D | E | F | G | H |...|
    | ---- |---|---|---|---|---|---|---|---|---|
    | 20:00| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |...|

    ### 现在方案及问题
    现在是通过跳过 n 行读取,然后拼接的方案,会存在多次调用 pandas.read_csv,性能太差了,想请教一个 python 大佬们有什么高效的办法
    ``pandas.read_csv(target_file, skiprows=skip_rows, nrows=range, skipinitialspace=True)
    skip_rows = skip_rows + range
    ``
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2699 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:57 · PVG 22:57 · LAX 07:57 · JFK 10:57
    ♥ Do have faith in what you're doing.