【问题标题】:How to ignore white spaces from a string in Python?如何在 Python 中忽略字符串中的空格?
【发布时间】:2018-12-16 04:32:15
【问题描述】:

我想为数据框中的列捕获“软件问题”。我的代码无法捕获在“SOFTWARE ISSUE”之间有多个空格的第四行。

data['comment'].str.contains("\\bsoftware issue\\b", case = False)

O/P:

Out[53]:

0    False

1     True

2     True

3    False

4     True

Name: comment, dtype: bool


**comment**

software

software issue

found software issue at end

SOFTWARE    ISSUE

   IN SOFTWARE ISSUE ON

请就此提出建议,以便我能够捕获甚至中间有空格的单词。

【问题讨论】:

  • 这与python2.7和python3.x有什么关系?
  • 空格的意思是,b/w SOFTWAREISSUE,对吗?
  • @hygull : 是的,先生
  • @hygull : 你的意思是“comment: column”的数据吗?

标签: python python-3.x pandas


【解决方案1】:

您可以使用\s+ 来检测空间。

例如:

import pandas as pd
data = pd.DataFrame({"comment": ["software", "software issue", "found software issue at end", "SOFTWARE    ISSUE" ]})
print(data['comment'].str.contains("\\bsoftware\s+issue\\b", case = False))

输出:

0    False
1     True
2     True
3     True
Name: comment, dtype: bool

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 2020-05-23
    • 2014-10-22
    • 2014-05-29
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多