【发布时间】: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.
要求:
我想将所有客户聊天实例(例如 James 和 Roy 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