如何通过交替行将数据框分组

时间:2019-07-08 21:02:01

标签: python pandas dataframe

说我有一个聊天会话的以下数据框:

Session    Name    Content
1          John    Hi!
1          John    my name is John
1          Sarah   Hello John, how can I help?
1          John    I need to check my current balance.
1          Sarah   I can help you with that.
1          Sarah   may I have your account number?
1          Sarah   or your SSN?

我想合并所有连续的话语,使数据框看起来像这样:

Session    Name    Content
1          John    Hi! my name is John
1          Sarah   Hello John, how can I help?
1          John    I need to check my current balance.
1          Sarah   I can help you with that. may I have your account number? or your SSN?

我尝试使用groupby创建一个新列,如此处所示适当地标记每行,但没有成功!

Session    Name    ID    Content
1          John    0     Hi!
1          John    0     my name is John
1          Sarah   1     Hello John, how can I help?
1          John    2     need to check my current balance.
1          Sarah   3     I can help you with that.
1          Sarah   3     may I have your account number?
1          Sarah   3     or your SSN?

我还没有找到其他文章来解决类似的问题,但是我可能正在搜索错误的关键字。非常感谢您的投入。

0 个答案:

没有答案