Python 将空列添加到Spark DataFrame
如web上的中所述,向现有数据帧添加新列并不简单。不幸的是,拥有此功能很重要(即使在分布式环境中效率很低),尤其是在尝试使用Python 将空列添加到Spark DataFrame,python,apache-spark,dataframe,pyspark,apache-spark-sql,Python,Apache Spark,Dataframe,Pyspark,Apache Spark Sql,如web上的中所述,向现有数据帧添加新列并不简单。不幸的是,拥有此功能很重要(即使在分布式环境中效率很低),尤其是在尝试使用unionAll连接两个DataFrame时 将null列添加到DataFrame以促进unionAll,最优雅的解决方法是什么 我的版本是这样的: from pyspark.sql.types import StringType from pyspark.sql.functions import UserDefinedFunction to_none = UserDefi
unionAll
连接两个DataFrame
时
将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']))
这里您只需要一个文字和类型转换:
from pyspark.sql.functions import lit
new_df = old_df.withColumn('new_column', lit(None).cast(StringType()))
一个完整的例子:
df = sc.parallelize([row(1, "2"), row(2, "3")]).toDF()
df.printSchema()
## root
## |-- foo: long (nullable = true)
## |-- bar: string (nullable = true)
new_df = df.withColumn('new_column', lit(None).cast(StringType()))
new_df.printSchema()
## root
## |-- foo: long (nullable = true)
## |-- bar: string (nullable = true)
## |-- new_column: string (nullable = true)
new_df.show()
## +---+---+----------+
## |foo|bar|new_column|
## +---+---+----------+
## | 1| 2| null|
## | 2| 3| null|
## +---+---+----------+
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()
df = spark.range(1, 3).toDF('c')
df = df.withColumn('foo', F.lit(None).cast('string'))
df.printSchema()
# root
# |-- c: long (nullable = false)
# |-- foo: string (nullable = true)
df.show()
# +---+----+
# | c| foo|
# +---+----+
# | 1|null|
# | 2|null|
# +---+----+
在这里可以找到一个Scala等价物:我将把lit(None)转换为NullType,而不是StringType。因此,如果我们必须过滤掉该列上的非空行,那么可以很容易地按如下方式完成
df = sc.parallelize([Row(1, "2"), Row(2, "3")]).toDF()
new_df = df.withColumn('new_column', lit(None).cast(NullType()))
new_df.printSchema()
df_null = new_df.filter(col("new_column").isNull()).show()
df_non_null = new_df.filter(col("new_column").isNotNull()).show()
如果要强制转换到StringType,也要小心不要使用lit(“None”)(带引号),因为在col(“new_column”)上搜索带有筛选条件的记录时会失败。不带
的选项导入StringType
df = df.withColumn('foo', F.lit(None).cast('string'))
完整示例:
df = sc.parallelize([row(1, "2"), row(2, "3")]).toDF()
df.printSchema()
## root
## |-- foo: long (nullable = true)
## |-- bar: string (nullable = true)
new_df = df.withColumn('new_column', lit(None).cast(StringType()))
new_df.printSchema()
## root
## |-- foo: long (nullable = true)
## |-- bar: string (nullable = true)
## |-- new_column: string (nullable = true)
new_df.show()
## +---+---+----------+
## |foo|bar|new_column|
## +---+---+----------+
## | 1| 2| null|
## | 2| 3| null|
## +---+---+----------+
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()
df = spark.range(1, 3).toDF('c')
df = df.withColumn('foo', F.lit(None).cast('string'))
df.printSchema()
# root
# |-- c: long (nullable = false)
# |-- foo: string (nullable = true)
df.show()
# +---+----+
# | c| foo|
# +---+----+
# | 1|null|
# | 2|null|
# +---+----+
如果列首先不存在,如何有条件地执行此操作?我试图使用UDF并将DF传递给它,然后执行
new_column not in DF.columns
检查,但无法使其正常工作。我也查看了它,但我仍然无法有条件地将其合并到with column('blah',where(has_column(DF['blah')==False)中..
一种构造。必须缺少一些语法构造。如果不存在,我想添加一个带null的列。这个答案是前者,另一个检查后者。@Gopaladf if has_column(df)else df.withColumn(..)
-没有特定于Spark的东西。该死……我真的搞混了python语法何时起作用,何时不起作用。例如,您不能在withColumn
中使用条件代码,并且必须使用UDF。谢谢!错误:拼花数据源不支持空数据类型。
StringType()
起作用。