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

2018-11-15 20:41:09 +08:00
 youthfire

这条语句用于实现无论查找件号,还是查找描述,只要完全匹配,则后续通过 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')

求教问题出在哪里

2895 次点击
所在节点    Python
3 条回复
billgreen1
2018-11-15 20:45:15 +08:00
keystring in df['code'] 有点类似 'a' in ['a', 'b','c']

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

df["code"].str.contains(keystring) | df["description"].str.contains(keystring)
youthfire
2018-11-15 20:52:39 +08:00
感觉两位的解释和纠正,已测,确实可行。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/508270

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX