【问题标题】:Check if one column value is in another column in pandas检查一列值是否在熊猫的另一列中
【发布时间】:2020-01-10 04:01:27
【问题描述】:

我想将同一数据框中的一列与另一列进行比较。不只是在相邻值之间,而是遍历 Col1 中的每个值,看看它是否存在于 Col2 值中。

Col1        Col2    exists
cat         pig     true
a           cat     false
pig         b       true
mat         axe     false

谢谢。

【问题讨论】:

  • df["exists"] = df["Col1"].isin(df["Col2"]),

标签: python-3.x pandas dataframe compare


【解决方案1】:
Col1_value = set(df['Col1'].unique())
df['exists'] = df['Col2'].map(lambda x : True if x in Col1_value  else False) 

【讨论】:

  • 仅代码的答案很少有帮助。请评论您的代码或解释为什么这是问题的解决方案。
猜你喜欢
  • 2021-09-02
  • 2020-05-08
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 2021-10-14
相关资源
最近更新 更多