将多个句子在python pandas中分词为行

2024-09-27 23:26:01 发布

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

我有一个这样的文本数据框

id      text
1       Thanks.  I appreciate your help.  I really like this chat service as it is very convenient.  I hope you have a wonderful day! thanks!
2       Got it. Thanks for the help; good nite.

我想把这些文本句子分开,并将它们与每个id匹配。我的预期输出是

id      text
1       Thanks.
1       I appreciate your help.
1       I really like this chat service as it is very convenient.
1       I hope you have a wonderful day!
1       thanks!
2       Got it.
2       Thanks for the help;
2       good nite.

是否有任何nltk函数可以处理此问题


Tags: text文本idyourisasservicechat
1条回答
网友
1楼 · 发布于 2024-09-27 23:26:01

首先split,然后使用explode,如果您没有将pandas升级到0.25,请选中How to unnest (explode) a column in a pandas DataFrame?

df.assign(text=df.text.str.split('[.!;]')).explode('text').loc[lambda x : x.text!='']
Out[181]: 
                                                text  id
0                                             Thanks   1
0                             I appreciate your help   1
0    I really like this chat service as it is ver...   1
0                    I hope you have a wonderful day   1
0                                             thanks   1
1                                             Got it   2
1                                Thanks for the help   2
1                                          good nite   2

相关问题 更多 >

    热门问题