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 · Jun 18, 2020 · 2317 views
    This topic created in 2139 days ago, the information mentioned may be changed or developed.

    df_new.loc[df_new.index>= df_new['low'].idxmin() ].loc[df_new['mark']==-1].head(1).index 这样写感觉太繁琐了,筛选应该是 loc 但 doc 似乎没发现有能同时设定行列的说明

    目的是得到在 low 列的最小值以后,mark 列值为-1 的一个数的序列号

    3 replies    2020-06-18 22:46:27 +08:00
    jyyx
        1
    jyyx  
       Jun 18, 2020
    mask = (df_new.index >= df_new['low'].idxmin()) & (df_new['mark'] == -1)
    new_df.loc[mask, :].head(1).index
    jyyx
        2
    jyyx  
       Jun 18, 2020   ❤️ 1
    其实获取 index 不需要用 loc 了
    mask = (df_new.index >= df_new['low'].idxmin()) & (df_new['mark'] == -1)
    mask.head(1).index
    billgreen1
        3
    billgreen1  
       Jun 18, 2020 via iPhone   ❤️ 1
    df.query (“mark==-1”).sort_values ( by=“low”).head ( 1 )
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5624 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 08:36 · PVG 16:36 · LAX 01:36 · JFK 04:36
    ♥ Do have faith in what you're doing.