推荐学习书目
› 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 · 1933 views
    This topic created in 2056 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   ·   Privacy   ·   Solana   ·   2544 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 08:18 · PVG 16:18 · LAX 01:18 · JFK 04:18
    ♥ Do have faith in what you're doing.