【发布时间】:2016-01-07 10:15:06
【问题描述】:
正如网络上的manyother locations 中所述,向现有 DataFrame 添加新列并不简单。不幸的是,拥有此功能很重要(即使它在分布式环境中效率低下),尤其是在尝试使用 unionAll 连接两个 DataFrames 时。
将null 列添加到DataFrame 以促进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']))
【问题讨论】:
标签: python apache-spark dataframe pyspark apache-spark-sql