比较两个不同长度的列

时间:2019-09-25 14:49:55

标签: pyspark pyspark-dataframes

我正在使用两个pyspark数据框,每个都有一列。一个带有3行(ColumnA),另一个带有100行(ColumnB)。 我想将ColumnA的所有行与ColumnB的每行进行比较。 (我需要知道ColumnA中的日期是否大于ColumnB中的日期,如果是,则在ColumnX中添加一个1)

任何建议将不胜感激。谢谢!

enter image description here

1 个答案:

答案 0 :(得分:0)

交叉加入是一种解决方案-

例如-

from pyspark.sql.types import *      
from pyspark.sql.functions import *  

A = [11, 2, 13, 4]
B = [5, 6]

df = spark.createDataFrame(A,IntegerType())
df1 = spark.createDataFrame(B,IntegerType())
df.select(col("value").alias("A")).crossJoin(df1.select(col("value").alias("B"))).withColumn("C",when(col("A") > col("B"),1).otherwise(0)).select("A","B","C").show()

+---+---+---+
|  A|  B|  C|
+---+---+---+
| 11|  5|  1|
| 11|  6|  1|
|  2|  5|  0|
|  2|  6|  0|
| 13|  5|  1|
| 13|  6|  1|
|  4|  5|  0|
|  4|  6|  0|
+---+---+---+
相关问题