gpt4 book ai didi

python - 根据另一列的顺序将字符串与 pandas GroupBy 连接起来

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:21 24 4
gpt4 key购买 nike

我的数据框有以下数据

callerid  seq   text
1236 2 I need to talk to x
1236 6 Issue 3 is this
1236 3 This is regarding abc
1236 5 Issue 2 is this
1236 4 Issue 1 is this
1236 1 Hi
1347 2 I need to talk to x
1347 6 Issue 3 is this
1347 3 This is regarding abc
1347 5 Issue 2 is this
1347 4 Issue 1 is this
1347 1 Hi

我需要按callerid对数据进行分组,按seq排序,连接文本并写入另一个数据框

最终输出的数据应该是这样的

callerid        text    
1236 Hi I need to talk to X This is regarding abc Issue 1 is this Issue 2 is this Issue 3 is this
1347 Hi I need to talk to X This is regarding abc Issue 1 is this Issue 2 is this Issue 3 is this

我试过下面的代码

documentext = dataextract.sort_values(['callerid','seq']).groupby('callerid')

documenttext1 = documenttext[['callerid','text']]
documentext1 = (documenttext1.groupby('callerid')['text']
.apply(lambda x: ' '.join(set(x.dropna())))
.reset_index())

第一个语句没有给我完整的排序文本这是我得到的输出

callerid seq   text
1236 1 Hi
1236 2 I need to talk to x
1236 3 This is regarding abc
1347 1 Hi
1347 2 I need to talk to x
1347 3 This is regarding abc

在此感谢任何帮助

提前致谢

最佳答案

如你所料,第一步是排序,第二步是分组。您可以使用 ' '.join 作为 aggfunc 来连接您的字符串。

(df.sort_values('seq')
.groupby('callerid', sort=False)['text']
.agg(' '.join)
.reset_index())

callerid text
0 1236 Hi I need to talk to x This is regarding abc I...
1 1347 Hi I need to talk to x This is regarding abc I...

您不应该对“seq”进行分组,因为您正试图对其 进行聚合。

关于python - 根据另一列的顺序将字符串与 pandas GroupBy 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56725840/

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