Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用下面的代码将空字符串转换为 Null。我没有收到错误,但它仍然是一个空字符串。 我怀疑变量col_name的使用不正确expr
col_name
expr
for col_name in ['col1', 'col2']: df_new = df \ .withColumn(col_name, F.expr(f"nullif('{col_name}', '')"))
你真正想做的是这个。请注意 for 循环将创建和覆盖df_new,因此仅更改最后一列。
df_new
from pyspark.sql import functions as f df_new = df for col_name in ['col1', 'col2']: df_new = df_new.withColumn(col_name, f.expr(f"nullif({col_name}, '')"))