gpt4 book ai didi

python - 将多个句子标记为 python pandas 中的行

转载 作者:行者123 更新时间:2023-12-02 16:50:10 25 4
gpt4 key购买 nike

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

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函数可以处理这个问题?

最佳答案

第一次 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

关于python - 将多个句子标记为 python pandas 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054043/

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