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 间隔行比较不同列的值

  •  
  •   yellowtail · 2020-07-02 17:44:55 +08:00 · 1797 次点击
    这是一个创建于 1392 天前的主题,其中的信息可能已经有所发展或是发生改变。
    dropmark flag high low trade_date mark markf


    1680 1.0 -1.0 113.2499 111.8426 2020-06-12 09:32:00 -1 -1
    1708 1.0 1.0 117.7255 117.5454 2020-06-12 10:00:00 1 1
    1740 1.0 -1.0 115.7845 115.5778 2020-06-12 10:32:00 -1 -1
    1871 1.0 1.0 117.3920 117.3787 2020-06-12 14:13:00 1 1
    1954 1.0 -1.0 116.6383 116.2781 2020-06-15 10:06:00 -1 -1
    2040 1.0 1.0 120.5869 120.3935 2020-06-15 13:02:00 1 1
    2146 1.0 -1.0 118.6593 118.5726 2020-06-15 14:48:00 -1 -1
    2251 1.0 1.0 120.1667 120.1534 2020-06-16 11:03:00 1 1
    2383 1.0 -1.0 120.2134 120.1600 2020-06-16 14:45:00 -1 -1
    2413 1.0 1.0 120.1000 120.0467 2020-06-17 09:45:00 1 1
    2465 1.0 -1.0 118.0590 118.0257 2020-06-17 10:37:00 -1 -1
    2474 1.0 1.0 118.6660 118.3525 2020-06-17 10:46:00 1 1
    2615 1.0 -1.0 117.6321 117.5654 2020-06-17 14:37:00 -1 -1
    2629 1.0 1.0 117.9790 117.8255 2020-06-17 14:51:00 1 1
    2758 1.0 -1.0 117.4720 117.4187 2020-06-18 11:30:00 -1 -1
    2772 1.0 1.0 117.9656 117.9256 2020-06-18 13:14:00 1 1
    2869 1.0 -1.0 118.8594 118.8594 2020-06-18 14:51:00 -1 -1
    2923 1.0 1.0 122.3211 122.0010 2020-06-19 10:15:00 1 1
    2953 1.0 -1.0 118.7927 118.7260 2020-06-19 10:45:00 -1 -1
    3063 1.0 1.0 120.4335 120.4268 2020-06-19 14:05:00 1 1
    3072 1.0 -1.0 119.8466 119.7265 2020-06-19 14:14:00 -1 -1
    3096 1.0 1.0 120.1267 120.0934 2020-06-19 14:38:00 1 1
    3151 1.0 -1.0 114.3972 114.2438 2020-06-22 10:03:00 -1 -1
    3268 1.0 1.0 118.6793 118.5793 2020-06-22 13:30:00 1 1
    3287 1.0 -1.0 117.5254 117.3320 2020-06-22 13:49:00 -1 -1
    3343 1.0 1.0 118.0523 117.9656 2020-06-22 14:45:00 1 1
    3357 1.0 -1.0 117.4587 117.0585 2020-06-22 14:59:00 -1 -1
    3376 1.0 1.0 124.9100 124.5500 2020-06-23 09:48:00 1 1
    3434 1.0 -1.0 120.5100 120.5000 2020-06-23 10:46:00 -1 -1
    3535 1.0 1.0 121.9800 121.9000 2020-06-23 13:57:00 1 1
    3914 1.0 -1.0 115.1000 115.0500 2020-06-29 10:46:00 -1 -1
    3972 1.0 1.0 116.1800 115.8500 2020-06-29 13:14:00 1 1
    4077 1.0 -1.0 114.2700 114.2600 2020-06-29 14:59:00 -1 -1
    4180 1.0 1.0 121.8400 121.6100 2020-06-30 11:12:00 1 1
    4209 1.0 -1.0 122.2600 121.8700 2020-06-30 13:11:00 -1 -1
    4236 1.0 1.0 123.7000 123.4100 2020-06-30 13:38:00 1 1
    4245 1.0 -1.0 122.5500 122.3300 2020-06-30 13:47:00 -1 -1

    要求找出是 markf 列值为-1 的值大于下一个行的 high 列值,用 for 循环感觉效率太低了...
    5 条回复    2020-07-03 10:30:54 +08:00
    czwstc
        1
    czwstc  
       2020-07-02 18:27:07 +08:00 via iPhone
    最后一句话我读了大概十几遍没看懂。
    你要么描述一下?
    cassidyhere
        2
    cassidyhere  
       2020-07-02 18:39:35 +08:00
    df['diff'] = df.high.shift() - df.high
    df[df.diff > 0][df.markf == -1]
    14cheese
        3
    14cheese  
       2020-07-02 19:19:15 +08:00 via Android
    DataFrame.shift 方法了解一下
    liangch
        4
    liangch  
       2020-07-02 21:00:28 +08:00
    shift(1) shift(-1)随你玩。
    yellowtail
        5
    yellowtail  
    OP
       2020-07-03 10:30:54 +08:00
    @czwstc 少打了一个 low 值...要求找出所有 markf 列值为-1 的,且大于下一个行的 high 列值,的 low 值
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2931 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:22 · PVG 23:22 · LAX 08:22 · JFK 11:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.