将PySpark DataFrame ArrayType字段组合到单个ArrayType字段中

zem*_*eng 12 python dataframe apache-spark apache-spark-sql pyspark

我有一个包含2个ArrayType字段的PySpark DataFrame:

>>>df
DataFrame[id: string, tokens: array<string>, bigrams: array<string>]
>>>df.take(1)
[Row(id='ID1', tokens=['one', 'two', 'two'], bigrams=['one two', 'two two'])]
Run Code Online (Sandbox Code Playgroud)

我想将它们组合成一个ArrayType字段:

>>>df2
DataFrame[id: string, tokens_bigrams: array<string>]
>>>df2.take(1)
[Row(id='ID1', tokens_bigrams=['one', 'two', 'two', 'one two', 'two two'])]
Run Code Online (Sandbox Code Playgroud)

使用字符串的语法似乎不起作用:

df2 = df.withColumn('tokens_bigrams', df.tokens + df.bigrams)
Run Code Online (Sandbox Code Playgroud)

谢谢!

zer*_*323 27

Spark> = 2.4

你可以使用concat功能(SPARK-23736):

from pyspark.sql.functions import col, concat 

df.select(concat(col("tokens"), col("tokens_bigrams"))).show(truncate=False)

# +---------------------------------+                                             
# |concat(tokens, tokens_bigrams)   |
# +---------------------------------+
# |[one, two, two, one two, two two]|
# |null                             |
# +---------------------------------+
Run Code Online (Sandbox Code Playgroud)

为了保持数据时的值之一是NULL,你可以coalescearray:

from pyspark.sql.functions import array, coalesce      

df.select(concat(
    coalesce(col("tokens"), array()),
    coalesce(col("tokens_bigrams"), array())
)).show(truncate = False)

# +--------------------------------------------------------------------+
# |concat(coalesce(tokens, array()), coalesce(tokens_bigrams, array()))|
# +--------------------------------------------------------------------+
# |[one, two, two, one two, two two]                                   |
# |[three]                                                             |
# +--------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

Spark <2.4

不幸的是,array在一般情况下连接列你需要一个UDF,例如:

from itertools import chain
from pyspark.sql.functions import col, udf
from pyspark.sql.types import *


def concat(type):
    def concat_(*args):
        return list(chain.from_iterable((arg if arg else [] for arg in args)))
    return udf(concat_, ArrayType(type))
Run Code Online (Sandbox Code Playgroud)

可以用作:

df = spark.createDataFrame(
    [(["one", "two", "two"], ["one two", "two two"]), (["three"], None)], 
    ("tokens", "tokens_bigrams")
)

concat_string_arrays = concat(StringType())
df.select(concat_string_arrays("tokens", "tokens_bigrams")).show(truncate=False)

# +---------------------------------+
# |concat_(tokens, tokens_bigrams)  |
# +---------------------------------+
# |[one, two, two, one two, two two]|
# |[three]                          |
# +---------------------------------+
Run Code Online (Sandbox Code Playgroud)


Dav*_*rba 6

在 Spark 2.4.0(Databricks 平台上的 2.3)中,您可以使用 concat 函数在 DataFrame API 中进行本机操作。在您的示例中,您可以这样做:

from pyspark.sql.functions import col, concat

df.withColumn('tokens_bigrams', concat(col('tokens'), col('bigrams')))
Run Code Online (Sandbox Code Playgroud)

是相关的jira。