Python에서 단어 길이로 텍스트 분할하려면 어떻게 하나요?

Asked 6 months ago, Updated 6 months ago, 102 views

text = "I have an Apple. I have a Banana. I have an Orange. I have a Watermelon."

이런 문장이 있다면 아래와 같이 5번째 단어마다 pandas dataframe으로 바꾸고 싶습니다.

id Text
0 I have an Apple. I
1 have a Banana. I have
2 an Orange I have a
3 Watermelon

좋은 방법이 있을까요? 

python pandas dataframe

2022-05-15 17:53

1 Answers

이렇게 한번 해보세요

text = "I have an Apple. I have a Banana. I have an Orange. I have a Watermelon."
words = text.split()
sentences = []
for i in range(0, len(words), 5):
    sentence = words[i:i+5]
    sentence = ' '.join(sentence)
    sentences.append(sentence)
series = pd.Series(sentences)
df = series.to_frame()
df.columns = ['Text']


2022-05-15 18:14

If you have any answers or tips


© 2022 pinfo. All rights reserved.