【发布时间】:2021-11-03 11:21:50
【问题描述】:
我有一个包含 3 列的数据框:分别为 'text', 'in', 'tar' 和 type(str, list, list)。
text in tar
0 This is an example text that I use in order to get an answer [2] [6]
1 Discussion: We are examining the possibility of this solution. [3] [6, 7, 8]
in 和 tar 表示我要标记到文本中的特定实体,它们返回每个找到的实体术语在文本中的位置。
例如,在in = [3] 所在的数据框的第二行,我从text 列中取出第三个单词(即:“正在检查”)并将其标记为<IN>examining</IN>。
同样,对于同一行,由于tar = [6,7, 8],我有<TAR>of</TAR>、<TAR>this</TAR>、<TAR>solution</TAR>。
但我想要的是当有连续的位置(即[1,2,3]或[6,7,8])在一个标签中together标记它们,例如@987654333 @。
我只想在位置连续(即:[1,2,3])时这样做,而不是在它们不连续时(即 [1,3,5])。
这是我目前所拥有的:
data = {'text': ['This is an example text that I use in order to get an answer',
'Discussion: We are examining the possibility of this solution'],
'in': [[2], [3]],
'tar': [[6], [6, 7, 8]]}
df = pd.DataFrame(data)
cols = list(df.columns)[1:]
new_text = []
for idx, row in df.iterrows():
temp = list(row['text'].split())
for pos, word in enumerate(temp):
for col in cols:
if pos in row[col]:
temp[pos] = f'<{col.upper()}>{word}</{col.upper()}>'
new_text.append(' '.join(temp))
df['text'] = new_text
print(df.text.to_list())
输出:
['This is <IN>an</IN> example text that <TAR>I</TAR> use in order to get an answer',
'Discussion: We are <IN>examining</IN> the possibility <TAR>of</TAR> <TAR>this</TAR> <TAR>solution</TAR>']
期望的输出:
['Discussion: We are <IN>examining</IN> the possibility <TAR>of this solution</TAR>']
有人可以帮忙吗?
【问题讨论】:
标签: python pandas dataframe nlp python-re