How to check if a value in one dataframe is present in keys in the other dataframe

后端 未结 2 695
半阙折子戏
半阙折子戏 2021-01-27 22:43

I have two dataframes:

df_1:

         Letters Boolean
          a         Nan
          b         Nan
          c         Nan

df_2:

相关标签:
2条回答
  • 2021-01-27 23:29

    Use numpy.where with isin:

    df1['Boolean'] = np.where(df1['Letters'].isin(df2.columns), 'x', np.nan)
    
    0 讨论(0)
  • 2021-01-27 23:37

    You need :

    df1['Boolean']=df1.Letters.isin(df2.columns).map({True:'x',False:np.nan})
    print(df1)
    
      Letters Boolean
    0       a       x
    1       b       x
    2       c     NaN
    
    0 讨论(0)
提交回复
热议问题