我想从python字符串中识别“如何”。
string1
how to find
it is a show
okay how to
使用过:
df[df['string1'].str.contains("how",case=False)]
输出即将到来:
string1
how to find
it is a show
okay how to
“显示”包含“如何”
需要的输出:
string1
how to find
okay how to
之后我就用
df[df['string1'].str.contains(r"\Whow",case=False)]
但是我得到的输出是:
string1
okay how to
再次磨损。
正则表达式“ \ W”不包含任何内容。
如何实现呢?
答案 0 :(得分:2)
模式需要边界(\b
),否则它也将匹配单词中包含的子字符串:
df[df['string1'].str.contains(r"\bhow\b",case=False)]
string1
1 how to find
3 okay how to
dtype: object
答案 1 :(得分:2)
使用\b
和正则表达式https://regex101.com/r/2Dlnxj/1将其作为单词边界
df['string1'].str.contains("\bhow\b",case=False)