Pandas GroupBy String正在连接列名而不是列值

2024-06-28 20:07:29 发布

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

我试图使用这个SO as guide对一个由DocID和一个字符串组成的DataFrame进行分组,但是不是一个每个DocID有一行的DataFrame,所有的字符串值都用空格隔开,最后我得到了一个包含列值的列。在

有人能指出我的错误吗?

样本数据

StringDF.head()

    DocID                                   LessStopWords
0   dd9ae7c8-7e98-4539-ab81-24c4780a6756    judgment of the court chamber 
1   dd9ae7c8-7e98-4539-ab81-24c4780a6756    the request proceedings
2   dd9ae7c8-7e98-4539-ab81-24c4780a6756    legal context law
3   dd9ae7c8-7e98-4539-ab81-24c4780a6756    article 1 directive
4   dd9ae7c8-7e98-4539-ab81-24c4780a6756    the status taken

我的代码

^{pr2}$

我的输出

^{3}$

我希望的输出

     DocID                                      LessStopWords
     0  010b158d-8c0b-49ad-9340-774893e4f62f    judgment of the court chamber the request proceedings legal context law article 1 directive
     1  02874037-416d-4b91-8e2d-1a288b8c3a7b    ...

Tags: ofthe字符串dataframerequestcontextlegaldocid
1条回答
网友
1楼 · 发布于 2024-06-28 20:07:29

{cd1>也可以使用^连接:

>>> df.groupby('DocID')['LessStopWords'].apply(lambda ser: ser.str.cat(sep=' '))
DocID
dd9ae7c8-7e98-4539-ab81-24c4780a6756    judgment of the court chamber the request proc...
Name: LessStopWords, dtype: object

更多示例请参见Working with Text Data。在


更大的例子:

^{pr2}$

相关问题 更多 >