【发布时间】:2019-03-23 14:22:21
【问题描述】:
我有一个跟踪多个客户和代表聊天的平台。我想创建包含单个客户的完整有序聊天历史记录的数据框。
为了快速参考:
Convo Room Date Message Order User ID Role Chat contents
A1 3-Oct-17 1 JOHN CUSTOMER Hi, can you help?
A1 4-Oct-17 2 ALICE REP Sure, what's up?
A1 5-Oct-17 3 JOHN CUSTOMER I have warts.
A1 6-Oct-17 4 JOHN CUSTOMER Please don't hang up, it's just warts.
B1 7-Oct-17 1 JOHN CUSTOMER Hi, can YOU help?
B1 8-Oct-17 2 MARY REP Sure, I heard about Alice.
B1 9-Oct-17 3 MARY REP I also have warts.
B1 10-Oct-17 4 JOHN CUSTOMER Oh, nevermind then, gotta go.
C1 7-Oct-17 1 JIM CUSTOMER Hi, can you help?
C1 8-Oct-17 2 ALICE REP Maybe, what's up?
C1 9-Oct-17 3 JIM CUSTOMER Not warts.
C1 10-Oct-17 4 ALICE REP Good, that's the only thing I cannot handle.
D1 15-Oct-17 1 JOHN CUSTOMER Hi, pls help. Warts.
D1 16-Oct-17 2 JUDE REP Perfect, I cure them!
D1 17-Oct-17 3 JUDE REP …with fire.
D1 18-Oct-17 4 JUDE REP Are you still there? Dang, lost another one.
在我看来,第一步是使用数据透视表对数据进行排序。接下来,我可以专注于将聊天分成数据框以进行情绪分析或其他指标。
我相信我已经很接近了,但我总是把排序的一部分弄错。
到目前为止我所拥有的:
df = test.pivot_table(index=['Role', 'User ID', 'Date', 'Convo Room', 'Message Order'],columns=["Role"],aggfunc='first')
df.head()
返回以下内容: 使用 Excel,我相信这通常是我想要的,尽管我确信有很多方法可以将其可视化:
【问题讨论】:
标签: python-3.x pandas group-by pivot-table