PySpark 列的withField(~) 方法用于添加或更新嵌套字段值。
参数
1. fieldName | string
嵌套字段的名称。
2. col | Column
要添加或更新的新列值。
返回值
PySpark 列 (pyspark.sql.column.Column)。
例子
考虑以下带有嵌套行的 PySpark DataFrame:
from pyspark.sql import Row
data = [
Row(name="Alex", age=20, friend=Row(name="Bob",age=30)),
Row(name="Cathy", age=40, friend=Row(name="Doge",age=40))
]
df = spark.createDataFrame(data)
df.show()
+-----+---+----------+
| name|age| friend|
+-----+---+----------+
| Alex| 20| {Bob, 30}|
|Cathy| 40|{Doge, 40}|
+-----+---+----------+
这里, friend 列包含嵌套的 Row ,可以通过打印模式来确认:
df.printSchema()
root
|-- name: string (nullable = true)
|-- age: long (nullable = true)
|-- friend: struct (nullable = true)
| |-- name: string (nullable = true)
| |-- age: long (nullable = true)
更新 PySpark 中的嵌套行
要更新嵌套行,请使用 withField(~) 方法,如下所示:
import pyspark.sql.functions as F
updated_col = df["friend"].withField("name", F.lit("BOB"))
df.withColumn("friend", updated_col).show()
+-----+---+---------+
| name|age| friend|
+-----+---+---------+
| Alex| 20|{BOB, 30}|
|Cathy| 40|{BOB, 40}|
+-----+---+---------+
请注意以下事项:
-
我们正在使用常量字符串
"BOB"更新friend列的name字段。 -
F.lit("BOB")返回一个Column对象,其值用字符串"BOB"填充。 -
withColumn(~)方法将 DataFrame 的friend列替换为withField(~)返回的更新列。
使用 PySpark 中的原始值更新嵌套行
要使用原始值更新嵌套行,请使用 withField(~) 方法,如下所示:
updated_col = df["friend"].withField("name", F.upper("friend.name"))
df.withColumn("friend", updated_col).show()
+-----+---+----------+
| name|age| friend|
+-----+---+----------+
| Alex| 20| {BOB, 30}|
|Cathy| 40|{DOGE, 40}|
+-----+---+----------+
在这里,我们使用 F.upper("friend.name") 将 friend 列的 name 字段大写,这会返回 Column 对象。
在 PySpark 的嵌套行中添加新字段值
withField(~) 列还可用于在嵌套行中添加新字段值:
updated_col = df["friend"].withField("upper_name", F.upper("friend.name"))
df_new = df.withColumn("friend", updated_col)
df_new.show()
+-----+---+----------------+
| name|age| friend|
+-----+---+----------------+
| Alex| 20| {Bob, 30, BOB}|
|Cathy| 40|{Doge, 40, DOGE}|
+-----+---+----------------+
现在,检查新的 PySpark DataFrame 的架构:
df_new.printSchema()
root
|-- name: string (nullable = true)
|-- age: long (nullable = true)
|-- friend: struct (nullable = true)
| |-- name: string (nullable = true)
| |-- age: long (nullable = true)
| |-- upper_name: string (nullable = true)
我们可以看到新的嵌套字段upper_name已添加!
相关用法
- Python PySpark Column isNotNull方法用法及代码示例
- Python PySpark Column getItem方法用法及代码示例
- Python PySpark Column rlike方法用法及代码示例
- Python PySpark Column cast方法用法及代码示例
- Python PySpark Column endswith方法用法及代码示例
- Python PySpark Column dropFields方法用法及代码示例
- Python PySpark Column alias方法用法及代码示例
- Python PySpark Column isNull方法用法及代码示例
- Python PySpark Column otherwise方法用法及代码示例
- Python PySpark Column contains方法用法及代码示例
- Python PySpark Column startswith方法用法及代码示例
- Python PySpark Column isin方法用法及代码示例
- Python PySpark Column substr方法用法及代码示例
- Python Collections.UserString用法及代码示例
- Python Collections.UserDict用法及代码示例
- Python Collections.UserList用法及代码示例
- Python Django Collate用法及代码示例
- Python Django ContentTypeManager用法及代码示例
- Python Condition release()用法及代码示例
- Python Condition notify()用法及代码示例
- Python Django ContextMixin.get_context_data用法及代码示例
- Python Condition wait()用法及代码示例
- Python Django Coalesce用法及代码示例
- Python Django Cot用法及代码示例
- Python Django CoordTransform用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 PySpark Column | withField method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
