V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
shuangchengsun
V2EX  ›  Python

Pandas 的性能问题

  •  1
     
  •   shuangchengsun · 2021 年 1 月 8 日 · 5310 次点击
    这是一个创建于 1829 天前的主题,其中的信息可能已经有所发展或是发生改变。
    DataFrame 的 append 操作每次都会返回一个全新的对象,势必会有大量的内存拷贝,这样岂不是会严重拉胯新能,还是说有新的姿势构造 DataFrame 。小白跪求大佬赐教。
    22 条回复    2021-01-10 10:45:44 +08:00
    lewinlan
        1
    lewinlan  
       2021 年 1 月 8 日 via Android
    我记得有一个参数,可以指定原地操作。
    wuwukai007
        2
    wuwukai007  
       2021 年 1 月 8 日 via Android
    concat
    bilibilifi
        3
    bilibilifi  
       2021 年 1 月 8 日 via iPhone
    list 里搞好了再转?我记得 dataframe 为了优化性能会把数据放在连续的内存里
    nthhdy
        4
    nthhdy  
       2021 年 1 月 8 日
    我不了解啊,瞎说的。但我感觉它不会拷贝内存的,有可能是写时复制的。可以读下源码看看。
    crystom
        5
    crystom  
       2021 年 1 月 8 日
    懒操作吧
    allAboutDbmss
        6
    allAboutDbmss  
       2021 年 1 月 8 日
    milkpuff
        7
    milkpuff  
       2021 年 1 月 9 日
    pandas 直接操作非常慢,一般拼接都 append 到 list 里面,最后一次性 concat 。
    pandas 索引、切片、拼接、赋值等都不适合频繁调用,转换成 numpy 效率可以极大提升
    Rorysky
        8
    Rorysky  
       2021 年 1 月 9 日
    猛男都直接用原生数据类型
    qile1
        9
    qile1  
       2021 年 1 月 9 日 via Android
    @milkpuff 多谢回复,我都是 for 遍历逐行附值到 pandas 里面,后期得修改下
    lixuda
        10
    lixuda  
       2021 年 1 月 9 日
    @qile1 apply 方式
    myCupOfTea
        11
    myCupOfTea  
       2021 年 1 月 9 日
    数据量太大确实会有性能问题啊
    底层是 numpy 吧,numpy concat 数据量大的阵列确实会很慢
    lithiumii
        12
    lithiumii  
       2021 年 1 月 9 日 via Android
    append 确实慢
    如果是一堆小 df 建议 concat 一个 list
    如果是一行一行的可以每行先存成一个 dict 然后 append 到 list 里,最后转成 df
    shuangchengsun
        13
    shuangchengsun  
    OP
       2021 年 1 月 9 日
    @bilibilifi 我现在的数据阵列是 rows 是一亿五千万左右,columns 是 12,有点太庞大了,用 list 的话能撑住么?
    shuangchengsun
        14
    shuangchengsun  
    OP
       2021 年 1 月 9 日
    @allAboutDbmss 谢谢推荐,我去看看这个工具
    shuangchengsun
        15
    shuangchengsun  
    OP
       2021 年 1 月 9 日
    @myCupOfTea 数据量确实很大,rows 差不多一亿五千万的样子。
    shuangchengsun
        16
    shuangchengsun  
    OP
       2021 年 1 月 9 日
    @lithiumii 感谢指导,我这就去试试。
    outoftimeerror
        17
    outoftimeerror  
       2021 年 1 月 9 日
    用 spark dataframe
    volvo007
        18
    volvo007  
       2021 年 1 月 9 日
    @shuangchengsun list 存的是对象指针,不是实际的对象……

    对于这个体量的数据,可以考虑用 vaex 这个库

    如果上下文操作不复杂且需要持久化的话,强烈安利 PostgreSQL 数据库来处理,超好用,真香
    weeevv
        19
    weeevv  
       2021 年 1 月 9 日
    Dask 或者直接上数据库吧。
    dlsflh
        20
    dlsflh  
       2021 年 1 月 9 日 via Android
    试试看并行的 pandas modin ?
    ElliotQi
        21
    ElliotQi  
       2021 年 1 月 9 日
    shuangchengsun
        22
    shuangchengsun  
    OP
       2021 年 1 月 10 日
    @volvo007 感谢指导
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1147 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:37 · PVG 01:37 · LAX 09:37 · JFK 12:37
    ♥ Do have faith in what you're doing.