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

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

  •  
  •   yellowtail · 2020-08-19 20:14:10 +08:00 · 1325 次点击
    这是一个创建于 1345 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://imgchr.com/i/d1DavD

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

    第 1 条附言  ·  2020-08-20 16:19:30 +08:00
    修改是所有 mark 列值为 1 的 high 列值 应大于 相邻 mark 列值为-1 的 low 列值..
    5 条回复    2020-08-20 16:32:54 +08:00
    wanv1171
        1
    wanv1171  
       2020-08-20 04:34:30 +08:00
    用 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
        2
    no1xsyzy  
       2020-08-20 09:30:30 +08:00   ❤️ 2
    shift 完可以整列进行比较、布尔运算,然后根据布尔值筛行
    apply 岂不是又回到 python 里运算了
    princelai
        3
    princelai  
       2020-08-20 10:24:02 +08:00   ❤️ 1
    condition = (data.low < data.high)&(data.low < data.high.shift(1))&(data.low < data.high.shift(-1))
    data.loc[condition]
    yellowtail
        4
    yellowtail  
    OP
       2020-08-20 16:18:46 +08:00
    @princelai 对不起...昨天发帖的时候不太清醒...我的意思是所有 mark 列值为 1 的 high 列值 应大于 相邻 mark 列值为-1 的 low 列值..
    yellowtail
        5
    yellowtail  
    OP
       2020-08-20 16:32:54 +08:00
    @princelai 已解决,在 condition 加上[2::2]..
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2835 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 00:25 · PVG 08:25 · LAX 17:25 · JFK 20:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.