从聊天记录中拆分Bot记录

2024-09-27 22:20:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有原始聊天机器人成绩单,在做任何情绪分析之前,我想把机器人记录从聊天记录中分离出来

数据已在dataframe中,如下所示:

Conversation_ID | Transcript
abcdef | BOT: Some text. CHATTER: Some text. BOT: Some text. BOT: Some text. CHATTER: Some text. BOT: Some text. BOT: Some text.

结果应该如下所示:

Conversation_ID | Transcript_BOT | Transcript_CHATTER
abcdef | Some text. Some text. Some text. Some text. Some text. | Some text. Some text.

Tags: 数据textiddataframebot记录机器人some
1条回答
网友
1楼 · 发布于 2024-09-27 22:20:22

如果我理解正确

df = pd.read_clipboard(sep='|')
df['Transcript'] = df['Conversation_ID'].str.split(':',expand=True)[1] # split by delim.
df['Conversation_ID'] = df['Conversation_ID'].str.split(':',expand=True)[0]
print(df)
    Conversation_ID Transcript
0   abcdef  None
1   BOT Some text.
2   CHATTER Some text.
3   BOT Some text.
4   BOT Some text.
5   CHATTER Some text.
6   BOT Some text.
7   BOT Some text.

为了你的预期结果:

new_df = (pd.crosstab(df['Conversation_ID'].iloc[0],
         df['Conversation_ID'].iloc[1:],
         values=df['Transcript']
        ,aggfunc='sum')).rename_axis('')
print(new_df)


Conversation_ID     BOT          CHATTER
abcdef              Some text. Some text. Some text. Some text. S...    Some text. Some text.

相关问题 更多 >

    热门问题