How to find if a spark column contains a certain value?

Viewed 2757

I have the following spark dataframe -

+----+----+
|col1|col2|
+----+----+
|   a|   1|
|   b|null|
|   c|   3|
+----+----+

Is there a way in spark API to detect if col2 contains, say, 3? Please note that the answer should be just one indicator value - yes/no - and not the set of records that have 3 in col2.

3 Answers

By counting the number of values in col2 that are equal to 3:

import pyspark.sql.functions as f
df.agg(f.expr('sum(case when col2 = 3 then 1 else 0 end)')).first()[0] > 0

You can use when as a conditional statement

from pyspark.sql.functions import when
df.select(
            (when(col("col2") == '3', 'yes')
            .otherwise('no')
            ).alias('col3')
          )
Related