在PySpark中使用这样的简单数据文件:
col1 col2 count
A 1 4
A 2 8
A 3 2
B 1 3
C 1 6我想复制行,以便使col1的每个值都与col2的每个值以及列计数填充0,对于那些我们没有原始值的值。会是这样的:
col1 col2 count
A 1 4
A 2 8
A 3 2
B 1 3
B 2 0
B 3 0
C 1 6
C 2 0
C 3 0你知道如何有效地做到这一点吗?
发布于 2018-05-15 16:01:52
你在找crossJoin。
data = df.select('col1', 'col2')
// this one gives you all combinations of col1+col2
all_combinations = data.alias('a').crossJoin(data.alias('b')).select('a.col1', 'b.col2')
// this one will append with count column from original dataset, and null for all other records.
all_combinations.alias('a').join(df.alias('b'), on=(col(a.col1)==col(b.col1) & col(a.col2)==col(b.col2)), how='left').select('a.*', b.count)https://stackoverflow.com/questions/50353999
复制相似问题