使用数据透视表跟踪用户聊天日志

时间:2019-03-23 14:22:21

标签: python-3.x pandas group-by pivot-table

我有一个跟踪多个客户和代表聊天的平台。我想创建一个数据框,其中包含单个客户的整个订购聊天记录。

这里是a link to the example data

快速参考:

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()

返回以下内容: almost but no cigar 使用Excel,我相信这通常是我想要的,尽管我敢肯定有很多方法可以将其可视化:

the goal

1 个答案:

答案 0 :(得分:0)

您处在正确的轨道上,只需确保将正确的列传递到<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/> <add name="Excel07+ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>

pivot_table