我有一个 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/