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

求教这个 pandas 中的模糊查询为何不生效

  •  
  •   youthfire · Nov 15, 2018 · 3990 views
    This topic created in 2876 days ago, the information mentioned may be changed or developed.

    这条语句用于实现无论查找件号,还是查找描述,只要完全匹配,则后续通过 df.loc 列出结果,这个没问题 df['group'] = np.where((df['code'] == keystring) | (df['description'] == keystring), 'hit', 'nohit')

    目前希望改进为,关键字的部分内容匹配序列中内容,就列出,相当于模糊查询,pycharm 没报错,但是不生效 df['group'] = np.where((keystring in df['code']) | (keystring in df['description']), 'hit', 'nohit')

    求教问题出在哪里

    3 replies  •  2018-11-15 20:52:39 +08:00
    billgreen1
        1
    billgreen1  
       Nov 15, 2018   ❤️ 1
    keystring in df['code'] 有点类似 'a' in ['a', 'b','c']

    你需要的是 df['ocde'].str.contains(keystring)
    ruoyu0088
        2
    ruoyu0088  
       Nov 15, 2018   ❤️ 1
    用这个试试看:

    df["code"].str.contains(keystring) | df["description"].str.contains(keystring)
    youthfire
        3
    youthfire  
    OP
       Nov 15, 2018
    感觉两位的解释和纠正,已测,确实可行。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2454 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 12:13 · PVG 20:13 · LAX 05:13 · JFK 08:13
    ♥ Do have faith in what you're doing.