【发布时间】:2018-10-27 16:55:20
【问题描述】:
我正在使用的查询:
我想在条件下用新值替换现有列,如果另一个 col = ABC 的值那么列保持不变,否则应该给出 null 或空白。 它根据逻辑给出结果,但仅针对它在循环中遇到的最后一列。
import pyspark.sql.functions as F
for i in df.columns:
if i[4:]!='ff':
new_df=df.withColumn(i,F.when(df.col_ff=="abc",df[i])\
.otherwise(None))
df:
+------+----+-----+-------+
| col1 |col2|col3 | col_ff|
+------+----+-----+-------+
| a | a | d | abc |
| a | b | c | def |
| b | c | b | abc |
| c | d | a | def |
+------+----+-----+-------+
需要的输出:
+------+----+-----+-------+
| col1 |col2|col3 | col_ff|
+------+----+-----+-------+
| a | a | d | abc |
| null |null|null | def |
| b | c | b | abc |
| null |null|null | def |
+------+----+-----+-------+
【问题讨论】:
-
请修正缩进
-
您每次都在循环内覆盖
new_df。
标签: pyspark pyspark-sql