gpt4 book ai didi

python - 合并每个 id 的后半行,但每个 id 的行不相同

转载 作者:行者123 更新时间:2023-12-01 01:19:23 25 4
gpt4 key购买 nike

attached the data image please go through我有一个包含两列的数据框,一列是“id”,另一列是“text”。每行包含一个 id 及其位于 2 个不同列中的文本。现在,我想合并每个 id 的后半部分文本行,对 id 进行分组。

    id      text
0 AB001 hello this is samrey how may I assist you
1 AB001 thank you for contacting chat helpline
2 AB001 could you please tell me your problem
3 AB002 Thank you for contacting ou team
4 AB002 agent will be assigned to you shortly
5 AB003 I will reset the the handset
6 AB003 switch off and switch on
7 AB003 still not working
8 AB003 it should work
9 AB003 now its working thanks a lot

最佳答案

您可以连接字符串,

new_df = df.groupby('id').text.apply(' '.join).reset_index()


id text
0 AB001 hello this is samrey how may I assist you tha...
1 AB002 Thank you for contacting ou team agent will b...
2 AB003 I will reset the the handset switch off and s...

编辑:根据评论,“假设一个 id 有 6 行,我们必须加入该特定 id 的最后 3 行”。以下将捕获行的后半部分并将它们连接起来。如果 id 有 3 行,则这将连接最后 2 行。

df.groupby('id').text.apply(lambda x: ''.join(x[(len(x)//2):])).reset_index()


id text
0 AB001 thank you for contacting chat helpline could you please tell me your problem
1 AB002 agent will be assigned to you shortly
2 AB003 still not working it should work now its working thanks a lot

关于python - 合并每个 id 的后半行,但每个 id 的行不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com