【问题标题】:Pyspark crossJoin with specific conditionPyspark crossJoin 与特定条件
【发布时间】:2020-10-25 17:47:37
【问题描述】:

每个 5 行的两个数据帧的交叉连接给出了一个 25 rows (5*5) 的数据帧。 我想要的是做一个 crossJoin 但它“不完整”。 例如:

df1:                       df2:
+-----+                    +-----+
|index|                    |value|
+-----+                    +-----+
|    0|                    |    A|
|    1|                    |    B|
|    2|                    |    C|
|    3|                    |    D|
|    4|                    |    E|
+-----+                    +-----+

结果必须是行数 index 中的每一行,随机选择 value 中用于完成交叉连接的行数。 会是这样的:

+-----+-----+
|index|value|
+-----+-----+
|    0|    D|
|    0|    A|
|    1|    A|
|    1|    D|
|    1|    B|
|    1|    C|
|    2|    A|
|    2|    E|
|    3|    D|
|    4|    A|
|    4|    B|
|    4|    E|
+-----+-----+

谢谢

【问题讨论】:

    标签: dataframe join pyspark cross-join


    【解决方案1】:

    您可以尝试使用sample(withReplacement, fraction, seed=None) 来减少交叉连接后的行数。

    Example:

    spark.sql("set spark.sql.crossJoin.enabled=true")
    df.join(df1).sample(False,0.6).show()
    

    【讨论】:

    • 正是我想要的。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多