You can use XOR to evaluate that only one of the conditions its true.
condition1 = F.col("Col1").like("%12345%")
condition2 = F.col("Col1").like("%67890%")
result = (condition1 & ~(condition2)) | (~(condition1) & condition2)
Output of: df.withColumn("Result", result).show()
:
Col1 |
Col2 |
Result |
12345;34567;67890 |
Text |
false |
34567;98765;65432 |
Text |
false |
12345;98765;65432 |
Text |
true |
pyspark when:
condition1 = F.col("Col1").like("%12345%")
condition2 = F.col("Col1").like("%67890%")
result = (
F.when(condition1 & ~condition2, "Condition 1 True")
.when(~condition1 & condition2, "Condition 2 True")
.otherwise("Neither condition is True")
)
Output of: df.withColumn("Result", result).show()
:
Col1 |
Col2 |
Result |
12345;34567;67890 |
Text |
Neither condition... |
34567;98765;65432 |
Text |
Neither condition... |
12345;98765;65432 |
Text |
Condition 1 True |
XOR works this way:
True ^ True == False
True ^ False == True
False ^ True == True
False ^ False == False
You can negate the value to obtain true if both of the conditions are the same value:
not (condition ^ condition)
Example:
not (True ^ True) == True
not (True ^ False) == False
not (False ^ True) == False
not (False ^ False) == True