gpt4 book ai didi

python - 句子被标记为 Pandas 中的单词,但索引从 0 到第一个句子的长度,对于第二个句子,它从 0 开始

转载 作者:行者123 更新时间:2023-12-04 09:06:47 24 4
gpt4 key购买 nike

我有一个 python df,

     DF1 = Index  Words    Tags
0 I O
1 Love B
2 India I
0 Flowers B
1 are O
2 Beautiful B
我希望输出是,
   DF1 = Index  Words    Tags   Sent
0 I O 1
1 Love B 1
2 India I 1
0 Flowers B 2
1 are O 2
2 Beautiful B 2
我想要一个 df

最佳答案

试试这个, pandas.DataFrame.Shift 创建一个 bool 掩码并应用 pandas.Series.cumsum .

df['Sent'] = (df.Index - df.Index.shift()).lt(0).cumsum() + 1
   Index      Words Tags  Sent
0 0 I O 1
1 1 Love B 1
2 2 India I 1
3 0 Flowers B 2
4 1 are O 2
5 2 Beautiful B 2

关于python - 句子被标记为 Pandas 中的单词,但索引从 0 到第一个句子的长度,对于第二个句子,它从 0 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63424430/

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