当前位置: 首页 > 面试题库 >

Python:UserWarning:此模式具有匹配组。要实际获得组,请使用str.extract

季骏祥
2023-03-14
问题内容

我有一个数据框,我尝试获取字符串,其中的列上包含一些字符串Df像

member_id,event_path,event_time,event_duration
30595,"2016-03-30 12:27:33",yandex.ru/,1
30595,"2016-03-30 12:31:42",yandex.ru/,0
30595,"2016-03-30 12:31:43",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:44",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:45",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:46",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:49",kinogo.co/,1
30595,"2016-03-30 12:32:11",kinogo.co/melodramy/,0

和另一个带有网址的df

url
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnyj_telefon_bq_phoenix
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnyj_telefon_fly_
003\.ru\/sonyxperia
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnye_telefony_smartfony
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnye_telefony_smartfony\/brands5D5Bbr_23
1click\.ru\/sonyxperia
1click\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/chasy-motorola

我用

urls = pd.read_csv('relevant_url1.csv', error_bad_lines=False)
substr = urls.url.values.tolist()
data = pd.read_csv('data_nts2.csv', error_bad_lines=False, chunksize=50000)
result = pd.DataFrame()
for i, df in enumerate(data):
    res = df[df['event_time'].str.contains('|'.join(substr), regex=True)]

但它还给我

UserWarning: This pattern has match groups. To actually get the groups, use str.extract.

我该如何解决?


问题答案:

中的至少一个正则表达式模式urls必须使用捕获组。 str.contains仅针对其中的每一行返回True或False
df['event_time']-不使用捕获组。因此,UserWarning警告您正则表达式使用捕获组,但未使用匹配项。

如果要删除,则UserWarning可以从正则表达式模式中找到并删除捕获组。它们没有显示在您发布的正则表达式模式中,但是它们必须在您的实际文件中。在字符类之外查找括号。

或者,您可以通过以下方式禁止此特定的UserWarning

import warnings
warnings.filterwarnings("ignore", 'This pattern has match groups')

在致电之前str.contains

这是一个简单的示例,演示了问题(和解决方案):

# import warnings
# warnings.filterwarnings("ignore", 'This pattern has match groups') # uncomment to suppress the UserWarning

import pandas as pd

df = pd.DataFrame({ 'event_time': ['gouda', 'stilton', 'gruyere']})

urls = pd.DataFrame({'url': ['g(.*)']})   # With a capturing group, there is a UserWarning
# urls = pd.DataFrame({'url': ['g.*']})   # Without a capturing group, there is no UserWarning. Uncommenting this line avoids the UserWarning.

substr = urls.url.values.tolist()
df[df['event_time'].str.contains('|'.join(substr), regex=True)]

版画

  script.py:10: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  df[df['event_time'].str.contains('|'.join(substr), regex=True)]

从正则表达式模式中删除捕获组:

urls = pd.DataFrame({'url': ['g.*']})

避免了UserWarning。



 类似资料:
  • 问题内容: 我使用Java中的正则表达式来捕获组,即使我知道表达式匹配,它也会不断抛出一个。 这是我的代码: 我期待是因为在正则表达式的捕获组拍摄的,而是我得到: IllegalStateException:找不到匹配项 我也尝试过,但发生相同的错误。 根据该文件,并: 捕获组从左到右从一个索引开始。零组表示整个模式,因此表达式等于。 我究竟做错了什么? 问题答案: 是帮助程序类,它处理数据迭代以

  • 函数组合 让我们创建两个函数: scala> def f(s: String) = "f(" + s + ")" f: (String)java.lang.String scala> def g(s: String) = "g(" + s + ")" g: (String)java.lang.String compose compose 组合其他函数形成一个新的函数 f(g(x)) scala>

  • 我正在我的react项目中使用react路由dom V5。我需要通过受保护的路由将道具从应用程序组件(我在其中导入路由器)传递到子组件。问题是道具是空的,没有匹配、位置和历史记录。。 **应用组件 注意:我将道具从应用程序传递到需要使用this.props.match的子组件,但match未定义

  • 问题内容: 想知道是否有一种方法可以在Swift中执行以下操作。 我知道我还有其他方法可以使这个人为的示例工作,例如是否使用开关,而是使用开关,但是我正在寻找一种解决方案,具体涉及将开关模式匹配到阵列。谢谢! 问题答案: 您可以定义一个自定义模式匹配运算符,该运算符 将一个数组作为“模式”和一个值: 类似的运算符已经存在,例如间隔:

  • 但我已经安装了Java14。

  • 我在angular 5中实现了一个模式,在.ts文件中使用以下代码进行密码验证。这样做的目的是支持至少八个字符,至少一个大写字母、一个小写字母、一位数字和一个特殊字符。请参阅:密码的Regex必须包含至少八个字符、至少一个数字以及大小写字母和特殊字符 我明白了,当我在密码文本框中输入一个字符串时,例如< code>Niladri1!然而,当我输入一个类似于< code>Nopasss123!!,它