python - 如何检查我的一列值是否存在于另一列中

标签 python pyspark

我有一个包含三列的 SQL 数据框

port    test1        test2
123     apple        ramesh eat apple
436     banana       banana is not a friute
467     cat 
78      tiger        cat is pet                     

我想找到 test1 列值存在于 test2 列值中。我想要这样的输出

port test1  test2                        check
123  apple  ramesh eat apple               1
436  banana banana is not a fruit          1
467  cat                                   0
78   tiger  cat is pet                     0

最佳答案

您可以使用contains函数来解决这个问题。这非常简单。

df = df.withColumn('check',when(col('test2').contains(col('test1')),1).otherwise(0))
df.show(truncate=False)

+----+------+---------------------+-----+
|port|test1 |test2                |check|
+----+------+---------------------+-----+
|123 |apple |ramesh eat apple     |1    |
|436 |banana|banana is not a fruit|1    |
|467 |cat   |null                 |0    |
|78  |tiger |cat is pet           |0    |
+----+------+---------------------+-----+

关于python - 如何检查我的一列值是否存在于另一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56186059/

相关文章:

Python子模块在python 3.7中正确导入但不是3.6

python - Windows 上程序的 CPU 使用率测量

python - 如果行中的任何值等于零,则在 pandas 数据框中删除行

apache-spark - pyspark.mllib DenseMatrix 乘法

python - 在 Python 的 ElementTree 中提取标签后的文本

python - numpy 标准化 4D 数组的 2D 子集

amazon-web-services - 在 AWS Glue pySpark 脚本中使用 SQL

python - 在分布式系统中实现DBSCAN

group-by - pyspark:聚合列中最常见的值

hadoop - pyspark : how to check if a file exists in hdfs