【问题标题】:Using Pivot Tables to track user chat log使用数据透视表跟踪用户聊天记录
【发布时间】:2019-03-23 14:22:21
【问题描述】:

我有一个跟踪多个客户和代表聊天的平台。我想创建包含单个客户的完整有序聊天历史记录的数据框。

这里是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()

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

【问题讨论】:

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


    【解决方案1】:

    你在正确的轨道上,只需要确保你将正确的列传递给pivot_table

    df.pivot_table(index=[ 'User ID', 'Convo Room', 'Message Order'],columns=["Role"],values='Chat contents',aggfunc='first')
    

    【讨论】:

    • 几乎,但这也不是完全可以做到的。用户 ID 已排序,但我似乎仍然无法让消息在用户 ID 内部来回传递。我不确定有没有办法重新引用以前引用过的数据透视表内部的相同用户 ID!
    猜你喜欢
    • 2012-03-28
    • 2013-11-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多