目标:
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,但它们对我而言并不起作用。
问题:
我如何实现目标?
答案 0 :(得分:1)
我们可以使用称为partition
的python内置方法
df['NextWord'] = df['Text'].apply(lambda x: x.partition('brca')[2]).str.split().str[0]
输出
Text Gene NextWord
0 carcinoma brca positive completion mastectomy brca positive
1 clinical brca gene mutation brca gene
2 carcinoma brca positive chemotherapy brca positive
说明
.partition
返回三个值:
string = 'carcinoma brca positive completion mastectomy'
before, keyword, after = string.partition('brca')
print(before)
print(keyword)
print(after)
输出
carcinoma
brca
positive completion mastectomy
我对答案之间的速度比较感到好奇,因为我使用了.apply
,但是它是一种内置方法。没想到,我的回答是最快的:
dfbig = pd.concat([df]*10000, ignore_index=True)
dfbig.shape
(30000, 2)
%%timeit
dfbig['Text'].apply(lambda x: x.partition('brca')[2]).str.split().str[0]
31.5 ms ± 1.36 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
dfbig['NextWord'] = dfbig['Text'].str.split('brca').str[1].str.split('\s').str[1]
74.5 ms ± 2.56 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
dfbig['NextWord'] = dfbig['Text'].str.extract(r"(?<=brca)(.+?) ")
40.7 ms ± 2.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
答案 1 :(得分:0)
大量使用熊猫Series.str
访问器:
df['NextWord'] = df['Text'].str.split('brca').str[1].str.split('\s').str[1]
df
Text Gene NextWord
0 carcinoma brca positive completion mastectomy brca positive
1 clinical brca gene mutation brca gene
2 carcinoma brca positive chemotherapy brca positive
答案 2 :(得分:0)
使用:
import pandas as pd
l = ['carcinoma brca positive completion mastectomy',
'clinical brca gene mutation',
'carcinoma brca positive chemotherapy']
df = pd.DataFrame(l, columns=['Text'])
df['NextWord'] = df['Text'].str.extract(r"(?<=brca)(.+?) ")
print(df)
输出:
Text NextWord
0 carcinoma brca positive completion mastectomy positive
1 clinical brca gene mutation gene
2 carcinoma brca positive chemotherapy positive