【问题标题】:Finding keyword +1 and making new column查找关键字+1并创建新列
【发布时间】: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 afterPANDAS 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


    【解决方案1】:

    我们可以利用python的内置方法partition

    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)
    

    【讨论】:

      【解决方案2】:

      大量使用 pandas 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
      

      【讨论】:

        【解决方案3】:

        用途:

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-26
          • 1970-01-01
          • 1970-01-01
          • 2020-10-28
          • 1970-01-01
          相关资源
          最近更新 更多