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
yellowtail
V2EX  ›  Python

pandas 赋值代码运算速度优化,麻烦给一点意见,谢谢!

  •  
  •   yellowtail · Nov 2, 2020 · 1557 views
    This topic created in 2002 days ago, the information mentioned may be changed or developed.

    '''

    def fuc_compare(df, i):

    try:
        if (df.loc[i+1].h > df.loc[i].h
             and df.loc[i+1].l > (df.loc[i].l + (df.loc[i].h - df.loc[i].l)/2)):
    
            df.loc['deal',i+1] = 1
    
            return
        if (df.loc[i+1].l < df.loc[i].l
             and df.loc[i+1].h < (df.loc[i].h - (df.loc[i].h - df.loc[i].l)/2)):
            df.loc['deal',i+1] = -1
            return
    except:
        print('^^^^^^^^^error^^^^^^^^^^^^')
        return
    

    i=0 while i < len(df_temp): fuc_compare(df_temp, i) i = i+1

    '''

    Supplement 1  ·  Nov 2, 2020

    '''

    while i < len(df_temp): fuc_compare(df_temp, i) i = i+1 '''

    Supplement 2  ·  Nov 2, 2020
    i=0

    while i < len(df_temp):


    fuc_compare(df_temp, i)


    i = i+1
    Supplement 3  ·  Nov 2, 2020
    最优的办法是 shift 值然后增加一列进行向量化运算
    yellowtail
        1
    yellowtail  
    OP
       Nov 2, 2020
    不知道为啥代码格式总不对,还在上班( zf 编外摸鱼发帖),先不改了。。
    yellowtail
        2
    yellowtail  
    OP
       Nov 2, 2020
    其实也就是 pandas 中需要跨行取值的问题,如果用 for i,n in df.iterrows():会快一些但是不知道,程序运行时间主要是花在哪一块... 如果 loc 操作相对于 for 循环不算高时间消耗的话
    也许有其他更好的办法..
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6028 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 158ms · UTC 03:22 · PVG 11:22 · LAX 20:22 · JFK 23:22
    ♥ Do have faith in what you're doing.