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

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

  •  
  •   youthfire · 2018-11-15 20:41:09 +08:00 · 2867 次点击
    这是一个创建于 1961 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这条语句用于实现无论查找件号,还是查找描述,只要完全匹配,则后续通过 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 条回复    2018-11-15 20:52:39 +08:00
    billgreen1
        1
    billgreen1  
       2018-11-15 20:45:15 +08:00   ❤️ 1
    keystring in df['code'] 有点类似 'a' in ['a', 'b','c']

    你需要的是 df['ocde'].str.contains(keystring)
    ruoyu0088
        2
    ruoyu0088  
       2018-11-15 20:47:04 +08:00   ❤️ 1
    用这个试试看:

    df["code"].str.contains(keystring) | df["description"].str.contains(keystring)
    youthfire
        3
    youthfire  
    OP
       2018-11-15 20:52:39 +08:00
    感觉两位的解释和纠正,已测,确实可行。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2861 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:59 · PVG 20:59 · LAX 05:59 · JFK 08:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.