通过


行类

DataFrame 中的行。 可以访问其中字段:

  • like attributes (row.key
  • 类似于字典值 (row[key]

key in row 将搜索行键。

行可用于使用命名参数创建行对象。 不允许省略命名参数来表示该值为 None 或缺失。 在这种情况下,这应显式设置为“无”。

Databricks Runtime 7.4 中已更改:从命名参数创建的行不再按字母顺序排序字段名称,并将按输入的位置排序。

Syntax

from pyspark.sql import Row

Row(tuple)

参数

参数 类型 说明
tuple 元组 行元素

方法

方法 说明
asDict(recursive) 将行返回为 Dict[str, Any].

示例

使用命名参数

from pyspark.sql import Row
row = Row(name="Alice", age=11)
row
# Row(name='Alice', age=11)
row['name'], row['age']
# ('Alice', 11)
row.name, row.age
# ('Alice', 11)
'name' in row
# True
'wrong_key' in row
# False

创建行类

行还可用于创建另一个类似 Row 的类,然后可用于创建行对象:

Person = Row("name", "age")
Person
# <Row('name', 'age')>
'name' in Person
# True
'wrong_key' in Person
# False
Person("Alice", 11)
# Row(name='Alice', age=11)

此窗体还可用于使用未命名字段创建行作为元组值:

row1 = Row("Alice", 11)
row2 = Row(name="Alice", age=11)
row1 == row2
# True