【问题标题】:Regex and pandas: extract partial string on name match正则表达式和熊猫:在名称匹配时提取部分字符串
【发布时间】:2018-10-31 02:21:17
【问题描述】:

我有一个 pandas 数据框,其中包含两个人(客户和服务台操作员)之间的网络聊天实例。

当客户进入对话时,客户姓名总是在网络聊天的第一行公布。

示例 1:

在:df['log'][0]

输出:[14:40:48] You are joining a chat with James[14:40:48] James: Hello, I\'m looking to find out more about the services and products you offer.[14:41:05] Greg: Thank you for contacting us. [17:41:14] Greg: Could I start by asking what services lines or products you are interested in knowing more about, please?[14:41:23] James: I would like to know more about your gardening and guttering service.[14:43:20] James: hello?[14:43:32] Greg: thank you, for more information on those please visit www.example.com/more_examples.[14:44:12] James: Thanks[14:44:38] James has exited the session.

示例 2:

在:df['log'][1]

输出:[09:01:25] You are joining a chat with Roy Andrews[09:01:25] Roy Andrews: I\'m asking on behalf of partner whether she is able to still claim warranty on a coffee machine she purchased a year and a half ago? [09:02:00] Jamie: Thank you for contacting us. Could I start by asking for the type of coffee machine purchased please, and whether she still has a receipt?[09:02:23] Roy Andrews: BRX0403, she no longer has a receipt.[09:05:30] Jamie: Thank you, my interpretation is that she would not be able to claim as she is no longer under warranty. [09:08:46] Jamie: for more information on our product warranty policy please see www.brandx.com/warranty-policy/information[09:09:13] Roy Andrews: Thanks for the links, I will let her know.[09:09:15] Roy Andrews has exited the session.

聊天中的名称总是随着不同客户使用网络聊天服务而变化。

客户可以输入具有一个或多个名称的聊天。例子: James Ravi Roy Andrews.

要求:

我想将所有客户聊天实例(例如 JamesRoy Andrews 的聊天)从 df['log'] 列分离到一个新列 df[text_analysis]

从上面的示例 1 看起来像:

在:df['text_analysis][0]

输出:[14:40:48] You are joining a chat with James[14:40:48] James: Hello, I\'m looking to find out more about the services and products you offer.[14:41:23] James: I would like to know more about your gardening and guttering service.[14:43:20] James: hello?[14:44:12] James: Thanks

编辑: 最佳解决方案将提取上面示例中提供的子字符串并省略最终时间戳[14:44:38] James has exited the session.

到目前为止我所做的尝试: 我已将 df['log'] 列中的客户名称提取到名为 df['names'] 的新列中,使用:

df['names'] = df['log'].apply(lambda x: x.split(' ')[7].split('[')[0])

我想在str.split() pandas 函数中使用df['names'] 列中的名称——类似于:

df['log'].str.split(df['names']) 但是这不起作用,如果在这种情况下确实发生了拆分,我认为它不会正确地将客户和服务运营商的聊天分开。

我还尝试将名称合并到正则表达式类型解决方案中:

df['log'].str.extract('([^.]*{}[^.]*)').format(df['log']))

但这也不起作用(因为我猜.extract() 不支持格式。

任何帮助将不胜感激。

【问题讨论】:

    标签: python regex string pandas


    【解决方案1】:

    使用regexlongs是你的第一段:

    import re
    re.match(r'^.*(?=\[)', longs).group()
    

    结果:

    "[14:40:48] You are joining a chat with James[14:40:48] James: Hello, I'm looking to find out more about the services and products you offer.[14:41:05] Greg: Thank you for contacting us. [17:41:14] Greg: Could I start by asking what services lines or products you are interested in knowing more about, please?[14:41:23] James: I would like to know more about your gardening and guttering service.[14:43:20] James: hello?[14:43:32] Greg: thank you, for more information on those please visit www.example.com/more_examples.[14:44:12] James: Thanks"

    您可以将此正则表达式函数打包到您的数据框中:

    df['text_analysis'] = df['log'].apply(lambda x: re.match(r'^.*(?=\[)', x).group())
    

    说明:正则表达式字符串'^.*(?=\[)'表示:从^开始,匹配任意数量的任意字符.*,以[结尾但不包括(?=\[)。由于正则表达式匹配最长的字符串,这将从开头一直到最后一个[,并且不包括[

    可以这样提取单独的行:

    import re
    customerspeak = re.findall(r'(?<=\[(?:\d{2}:){2}\d{2}\]) James:[^\[]*', s)
    

    输出:

    [" James: Hello, I'm looking to find out more about the services and products you offer.",
     ' James: I would like to know more about your gardening and guttering service.',
     ' James: hello?',
     ' James: Thanks']
    

    如果你想要这些在同一行,你可以''.join(customerspeak)

    【讨论】:

    • 您好,感谢您的解决方案——但这只会删除最后一个时间戳,也不会拆分聊天的客户实例。有没有办法扩展您当前的解决方案以满足全部要求?
    • 已更新,请查收。
    • @RockyLi 最好使用Series.str 方法而不是apply-ing 一个re 函数。 pandas.pydata.org/pandas-docs/stable/text.html
    • @PaulH 你对列表输出有什么想法吗?即findall 并加入?
    • 试试expand=True 选项
    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 2020-01-15
    • 2021-03-06
    • 2021-02-09
    • 2018-08-28
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多