【发布时间】:2019-10-13 07:09:30
【问题描述】:
目标:
1) 定位关键字旁边的单词(例如brca)
2) 用这个词创建一个新列
背景:
1) 我有一个列表l,我在其中创建了一个数据框df,并使用以下代码从中提取单词brca:
l = ['carcinoma brca positive completion mastectomy',
'clinical brca gene mutation',
'carcinoma brca positive chemotherapy']
df = pd.DataFrame(l, columns=['Text'])
df['Gene'] = df['Text'].str.extract(r"(brca)")
输出:
Text Gene
0 breast invasive lobular carcinoma brca positiv... brca
1 clinical history brca gene mutation . gross de... brca
2 left breast invasive ductal carcinoma brca pos... brca
问题:
但是,我现在尝试为每一行查找单词 brca 旁边的单词并创建一个新列。
所需的输出:
Text Gene NextWord
0 breast invasive lobular carcinoma brca positiv... brca positive
1 clinical history brca gene mutation . gross de... brca gene
2 left breast invasive ductal carcinoma brca pos... brca positive
我查看了python pandas dataframe words in context: get 3 words before and after 和PANDAS Finding the exact word and before word in a column of string and append that new column in python (pandas) column,但它们对我不太适用。
问题:
我如何实现我的目标?
【问题讨论】:
标签: regex pandas text nlp keyword