从Chatter记录中拆分Bot记录

时间:2019-06-12 20:39:59

标签: python pandas nlp chat

我有原始的聊天机器人副本,在进行任何情感分析之前,我想将Bot记录与Chatter记录分开。

数据已存在于数据框中,如下所示:

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.

1 个答案:

答案 0 :(得分:0)

如果我理解正确,

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.