使用 PySpark 迭代 SQL.Row 的列表

Iterating List of SQL.Row with PySpark

我有一个 Spark.SQL.Row 看起来像这样:

my_row = Row(id = 1,
    value = [Row(id = 1, value = "value1"), Row(id = 2, value = "value2")])

我想使用如下方法从每个嵌套行中获取值:

[x.value for x in my_row.value]

问题是当我迭代时,整行被转换成元组,

my_row = (1, [(1, "value1"), (2, "value2")])

我失去了模式。有没有一种方法可以迭代并保留行列表的架构?

准确的说pyspark.sql.row其实是一个tuple:

isinstance(my_row, tuple)
# True

由于 Python 元组是不可变的,我看到的唯一选择是从头开始重建 Row

d = my_row.asDict()
d["value"] = [Row(value=x.value) for x in  my_row.value]
Row(**d)

## Row(id=1, value=[Row(value='value1'), Row(value='value2')])