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

pandas 相关条件筛选相关,怎么同时对行和列进行条件限定有更好的写法吗

  •  
  •   yellowtail · 2020-06-18 15:28:51 +08:00 · 1453 次点击
    这是一个创建于 1407 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    3 条回复    2020-06-18 22:46:27 +08:00
    jyyx
        1
    jyyx  
       2020-06-18 16:06:39 +08:00
    mask = (df_new.index >= df_new['low'].idxmin()) & (df_new['mark'] == -1)
    new_df.loc[mask, :].head(1).index
    jyyx
        2
    jyyx  
       2020-06-18 16:17:50 +08:00   ❤️ 1
    其实获取 index 不需要用 loc 了
    mask = (df_new.index >= df_new['low'].idxmin()) & (df_new['mark'] == -1)
    mask.head(1).index
    billgreen1
        3
    billgreen1  
       2020-06-18 22:46:27 +08:00 via iPhone   ❤️ 1
    df.query (“mark==-1”).sort_values ( by=“low”).head ( 1 )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 13:15 · PVG 21:15 · LAX 06:15 · JFK 09:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.