简体   繁体   English

将数据帧的值转换为 Python 中的句子

[英]Convert values of a data-frame into sentence in Python

I have the following dataframe in python: enter image description here我在 python 中有以下 dataframe:在此处输入图像描述

I would like to convert from word ok(index = 0) to something(index =24) into one sentence based on end_sentence column value which is 1 to indicate end of a sentence.我想根据 end_sentence 列值将单词 ok(index = 0) 转换为 something(index =24) 为一个句子,该值是 1 表示句子的结尾。 Similar from Night(index=25) to significant (index=41) into another sentence从 Night(index=25) 到显着 (index=41) 相似到另一个句子

sentences = []
indices = df.index[df['end_sentence'] == 1]

begin = 0
for i in indices:
    end = i + 1
    sentence = ' '.join(list(df['word'][begin:end]))
    sentences.append(sentence)
    begin = end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM