How to check whether the content of Column A is contained in Column B using Python DataFrame?

后端 未结 2 699
野趣味
野趣味 2020-12-21 06:18

I have two columns in a pandas DataFrame: authors and name. I want to create a third column: a cell\'s value is True if the correspond

2条回答
  •  长情又很酷
    2020-12-21 07:14

    Here is a vectorized solution, which uses Series.str.split() and DataFrame.isin() methods:

    df['Check'] = df.Authors.str.split(r'\s*,\s*', expand=True).isin(df.Name).any(1)
    

    Demo:

    In [126]: df
    Out[126]:
                     Authors     Name
    0  S.Rogers, T. Williams   H. Tov
    1      M. White, J.Black  J.Black
    
    In [127]: df.Authors.str.split(r'\s*,\s*', expand=True)
    Out[127]:
              0            1
    0  S.Rogers  T. Williams
    1  M. White      J.Black
    
    In [128]: df.Authors.str.split(r'\s*,\s*', expand=True).isin(df.Name)
    Out[128]:
           0      1
    0  False  False
    1  False   True
    
    In [130]: df['Check'] = df.Authors.str.split(r'\s*,\s*', expand=True).isin(df.Name).any(1)
    
    In [131]: df
    Out[131]:
                     Authors     Name  Check
    0  S.Rogers, T. Williams   H. Tov  False
    1      M. White, J.Black  J.Black   True
    

提交回复
热议问题