【发布时间】:2021-10-20 08:51:03
【问题描述】:
考虑这个简单的例子
import pandas as pd
df = pd.DataFrame({'col1' : [1,2,3],
'col2' : ['A','B','C'],
'paragraph': ['sentence one. sentence two',
'sentence three. and sentence four',
'crazy sentence!! and the final one.']})
df
Out[11]:
col1 col2 paragraph
0 1 A sentence one. sentence two
1 2 B sentence three. and sentence four
2 3 C crazy sentence!! and the final one.
我想将段落拆分成句子(最好使用spacy),但我需要将信息保留在其他列中。
我知道如何在. 上分解列和拆分(天真)
df.paragraph.str.split('.').explode()
Out[10]:
0 sentence one
0 sentence two
1 sentence three
1 and sentence four
2 crazy sentence!! and the final one
2
Name: paragraph, dtype: object
但这会丢失col1 和col2 中的信息(这些信息应在逐句数据框中保留和重复),并且不会正确地用感叹号拆分句子。
使用 Spacy 和 nlp(paragraph).sents 仍然会丢失两列。
我能做什么? 谢谢!
【问题讨论】: