【发布时间】:2019-06-06 00:11:12
【问题描述】:
谁能回答下面链接上的问题,但在 pyspark 中?
how to fill a column with the value of another column based on a condition on some other columns?
我在这里再次重复这个问题:
假设我们在pyspark中有一个dataframe如下:
col1 | col2 | col3 | col4
22 | null | 23 | 56
12 | 54 | 22 | 36
48 | null | 2 | 45
76 | 32 | 13 | 6
23 | null | 43 | 8
67 | 54 | 56 | 64
16 | 32 | 32 | 6
3 | 54 | 64 | 8
67 | 4 | 23 | 64
如果col4<col1 和col2 不是null,我想用col1 替换col4 的值
所以结果应该是
col1 | col2 | col3 | col4
22 | null | 23 | 56
12 | 54 | 22 | 36
48 | null | 2 | 45
76 | 32 | 13 | 76
23 | null | 43 | 8
67 | 54 | 56 | 67
16 | 32 | 32 | 16
3 | 54 | 64 | 8
67 | null | 23 | 64
任何帮助将不胜感激。
【问题讨论】:
-
使用来自
pyspark.sql.functions的when()函数,它的工作方式类似于if else子句。
标签: dataframe pyspark conditional-statements