【发布时间】:2021-06-29 10:57:47
【问题描述】:
我有一串文本,我想从中找到第 n 个单词。我可以通过 min 和 max 提取第一个和最后一个,但不知道如何获取中间的项目。
我的代码:
import pandas as pd
import numpy as np
data = {"Text" : ["['one', 'one two', 'four']","['two one', 'three', 'five]"]}
df = pd.DataFrame(data)
df["One"] = df["Text"].str.find("one")
df["Two"] = df["Text"].str.find("two")
df["Three"] = df["Text"].str.find("three")
df["Four"] = df["Text"].str.find("four")
df["Five"] = df["Text"].str.find("five")
score_words = df.loc[:,"One":"Five"]
score_words_dict = dict(
list(
score_words.groupby(score_words.index)
)
)
score_words = score_words[score_words >0]
df["AllScoreWords"] =""
for k, v in score_words_dict.items(): # k: name of index, v: is a df
df["AllScoreWords"][k] = str(v.columns[(v != -1).any()].to_list())
df['First_Score'] = score_words.idxmin(axis=1)
df['Last_Score'] = score_words.idxmax(axis=1)
print(df)
print(score_words)
所以在第一行中,我希望能够提取 Two 作为第二个记分词 & 在第二行中我想提取 One 作为第二个记分词.....等等。
实际上我有一个关键字紧随其后或之前我想拉出所说的话,所以简单地增加得分词的阈值是行不通的。
我怎样才能挑选出我想要的单词?
J
【问题讨论】:
标签: python string dataframe text