正如在许多 其他网站上提到的,向现有DataFrame添加新列并不简单。尽管在分布式环境中效率低下,但拥有此功能非常重要,特别是在尝试使用unionAll
连接两个DataFrame
时。
添加一个null
列以便于使用unionAll
,最优雅的解决方案是什么?
我的版本如下:
from pyspark.sql.types import StringType
from pyspark.sql.functions import UserDefinedFunction
to_none = UserDefinedFunction(lambda x: None, StringType())
new_df = old_df.withColumn('new_column', to_none(df_old['any_col_from_old']))
new_column not in df.columns
检查,但无法使其正常工作。 - GopalawithColumn('blah', where(has_column(df['blah']) == False)....
这种结构中。可能缺少某些语法结构。我想添加一个带有 Null 的列,如果它不存在的话。这个答案做了前者,另一个检查后者。 - Gopaladf if has_column(df) else df.withColumn(....)
- 没有Spark特定的内容。 - zero323withColumn
中不能有条件代码,必须使用UDF。谢谢! - Gopala