【问题标题】:How to extract matching keywords from two columns in a pandas dataframe?如何从熊猫数据框中的两列中提取匹配的关键字?
【发布时间】:2019-10-12 23:12:49
【问题描述】:

我在数据框中有两列都是字符串,其中 column1 在 column2 中有一些匹配的关键字。我想在新列中从 column1 和 column2 中提取那些匹配的关键字。

df['column3']=df.column1.apply(lambda x : df.column2[df.column2.str.contains(x)]

我期待这样的输出

column1                     column2                 column3
A girl is going to market   girl market school      girl market
A girl is going to school   girl market school      girl school
The sky is blue in color    sky blue orange color   sky blue color

【问题讨论】:

    标签: python-3.x pandas dataframe text-extraction


    【解决方案1】:

    使用apply

    例如:

    df["column3"] = df.apply(lambda x: " ".join(i for i in x["column2"].split() if i in x["column1"]),axis=1) 
    print(df)
    

    输出:

                         column1                column2         column3
    0  A girl is going to market     girl market school     girl market
    1  A girl is going to school     girl market school     girl school
    2   The sky is blue in color  sky blue orange color  sky blue color
    

    【讨论】:

      【解决方案2】:

      使用np.intersect1d

      df['column3'] = df.apply(lambda x: ' '.join(np.intersect1d(x['column1'].split(),x['column2'].split())), axis=1)
      

      输出

                           column1                    column2         column3
      0  A girl is going to market         girl market school     girl market
      1  A girl is going to school         girl market school     girl school
      2   The sky is blue in color      sky blue orange color  blue color sky
      

      如果顺序很重要

       df['column3'] = df.apply(lambda x: ' '.join(np.array(x['column1'].split())[np.in1d(x['column1'].split(),x['column2'].split())]), axis=1)
      

      输出

                           column1                    column2         column3
      0  A girl is going to market         girl market school     girl market
      1  A girl is going to school         girl market school     girl school
      2   The sky is blue in color      sky blue orange color  sky blue color
      

      【讨论】:

        【解决方案3】:

        使用sets的交集(&)的另一种解决方案:

        df['column3'] = df.apply(lambda x: ' '.join(set(x['column1'].split()) &
                                                    set(x['column2'].split())), axis=1)
        

        [出]

                             column1                column2         column3
        0  A girl is going to market     girl market school     market girl
        1  A girl is going to school     girl market school     girl school
        2   The sky is blue in color  sky blue orange color  sky color blue
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-18
          • 2021-10-06
          • 1970-01-01
          • 2021-01-02
          • 2022-01-23
          • 2021-01-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多