- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个带有 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'])]
我想将它们组合成一个 ArrayType 字段:
>>>df2
DataFrame[id: string, tokens_bigrams: array<string>]
>>>df2.take(1)
[Row(id='ID1', tokens_bigrams=['one', 'two', 'two', 'one two', 'two two'])]
适用于字符串的语法在这里似乎不起作用:
df2 = df.withColumn('tokens_bigrams', df.tokens + df.bigrams)
谢谢!
最佳答案
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 |
# +---------------------------------+
要在其中一个值为 NULL
时保留数据,您可以使用 array
coalesce
:
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] |
# +--------------------------------------------------------------------+
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))
可以用作:
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] |
# +---------------------------------+
关于python - 将 PySpark DataFrame ArrayType 字段组合成单个 ArrayType 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284077/
我有一个带有 2 个 ArrayType 字段的 PySpark DataFrame: >>>df DataFrame[id: string, tokens: array, bigrams: arra
我正在尝试运行一些测试。即 assertDataFrameEquals(etalon, agg) 在 2 个数据帧上。但是得到如下错误 ScalaTestFailureLocation Expecte
我有包含 2 个 ArrayType 列的数据框。我想找出列之间的区别。 column1 将始终具有值,而 column2 可能具有空数组。我创建了以下 udf,但它不起作用 df.show() 给出
我正在尝试创建一个与 ArrayType 一起使用的全局 findAndRemove 函数,但遇到了问题。我收到一条错误消息: 无法将表达式的类型“0”转换为“@lvalue Named”类型 这是函
extension Array where Element: _ArrayType, Element.Generator.Element: Any { func transpose() ->
这个问题在这里已经有了答案: TypeError: Column is not iterable - How to iterate over ArrayType()? (2 个答案) 关闭 3 年前
我在 Pyspark 数据框中有一列,结构如下 Column1 [a,b,c,d,e] [c,b,d,f,g,h,i,p,l,m] 我想返回另一列,随机选择每行中的每个数组,数量在函数中指定。 所以像
我正在尝试按元素求和,并且我创建了这个虚拟 df。输出应该是 [10,4,4,1] from pyspark.sql.types import StructType,StructField, Stri
我有一个包含可以包含整数值的数组类型列的数据框。如果没有值,它将只包含一个值,它将是空值 重要 :注意该列不会为空,而是具有单个值的数组;空值 > val df: DataFrame = Seq((
在 HDF5 的 C++ 绑定(bind)中,我可以创建一个 1x4 H5::ArrayType 由带有类似表达式的 double 组成 H5::ArrayType array_type(H5::Pr
我有一个以 XML 形式出现的数据集,其中一个节点包含 JSON。 Spark 将其作为 StringType 读取,因此我尝试使用 from_json() 将 JSON 转换为 DataFrame。
我正在尝试使用 ArrayType() 列创建一个新的数据框,我尝试过定义模式和不定义模式,但无法获得所需的结果。我的代码在下面,带有架构 from pyspark.sql.types import
我写了一个UDF。它非常慢。我想用 pandas_udf 替换它以利用矢量化。 实际的 udf 有点复杂,但我创建了一个简化的玩具版本。 我的问题:是否可以将玩具示例中的 UDF 替换为可以利用矢量化
我目前有以下代码: def _join_intent_types(df): mappings = { 'PastNews': 'ContextualInformation', 'C
给定以下形式的 PySpark DataFrame: +----+--------+ |time|messages| +----+--------+ | t01| [m1]| | t03|[m1
我正在尝试为我的新 DataFrame 创建一个架构,并尝试了括号和关键字的各种组合,但无法弄清楚如何完成这项工作。我目前的尝试: from pyspark.sql.types import * sc
我有一个 PySpark 数据框,其中一列是字符串类型,而字符串是一个 2D 数组/列表,需要分解为行。但是,由于它不是结构/数组类型,因此无法直接使用explode。 这可以在下面的示例中看到: a
我有一个包含 3 列名为 id、feat1 和 feat2 的数据框。 feat1 和 feat2 是字符串数组的形式: Id, feat1,feat2 ------------------ 1, [
我有一个 HDF5 数据集,它是使用 H5::ArrayType 为 double[3] 编写的。 DataSpace 是一维的 (rank=1),具有 ndat 条目(每个类型都是 double[3
我正在尝试编写一个 PostgreSQL 函数并存储一个 ArrayType *state,每次我调用该函数时都需要更改它。每次调用函数时,我是否需要在 construct_array 中使用单独的
我是一名优秀的程序员,十分优秀!