推荐学习书目
› 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

求教如何用简短代码发现一个 dataframe 里出现 a 列值大于相邻行的 b 列值情况的位置。。。

  •  
  •   yellowtail · Aug 19, 2020 · 2453 views
    This topic created in 2274 days ago, the information mentioned may be changed or developed.

    https://imgchr.com/i/d1DavD

    所有的 low 列的值不应该高于上下相邻行的 high 列的值 想过分别取两列值作为两个 list 然后相减但是觉得得减两次(上下关系各一次) 而且保留序号也很麻烦...

    Supplement 1  ·  Aug 20, 2020
    修改是所有 mark 列值为 1 的 high 列值 应大于 相邻 mark 列值为-1 的 low 列值..
    5 replies  •  2020-08-20 16:32:54 +08:00
    wanv1171
    wanv1171
    Aug 20, 2020
    1
    用 shift?

    df['Last_High'] = df['High'].shift(1)
    df['Next_High'] = df['High'].shift(-1)

    df['Low_Bigger_Than_High'] = df.apply(lambda row: row['Low'] > max(row['High'], row['Last_High'], row['Next_High']),axis = 1)
    no1xsyzy
    no1xsyzy
    Aug 20, 2020❤️2
    2
    shift 完可以整列进行比较、布尔运算,然后根据布尔值筛行
    apply 岂不是又回到 python 里运算了
    princelai
    princelai
    Aug 20, 2020❤️1
    3
    condition = (data.low < data.high)&(data.low < data.high.shift(1))&(data.low < data.high.shift(-1))
    data.loc[condition]
    yellowtail
    yellowtail
    OP
    Aug 20, 2020
    4
    @princelai 对不起...昨天发帖的时候不太清醒...我的意思是所有 mark 列值为 1 的 high 列值 应大于 相邻 mark 列值为-1 的 low 列值..
    yellowtail
    yellowtail
    OP
    Aug 20, 2020
    5
    @princelai 已解决,在 condition 加上[2::2]..
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3500 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 05:36 · PVG 13:36 · LAX 21:36 · JFK 00:36
    ♥ Do have faith in what you're doing.