Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Returns a new DataFrame in which null values are filled with a new value. DataFrame.fillna and DataFrameNaFunctions.fill are aliases of each other.
Syntax
fill(value, subset=None)
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
int, float, str, bool, or dict | The value to replace null values with. If a dict is provided, subset is ignored and value must be a mapping from column name to replacement value. Replacement values must be int, float, bool, or str. |
subset |
str, tuple, or list, optional | Column names to consider. Columns in subset that do not have a matching data type for value are ignored. |
Returns
DataFrame
Examples
df = spark.createDataFrame([
(10, 80.5, "Alice", None),
(5, None, "Bob", None),
(None, None, "Tom", None),
(None, None, None, True)],
schema=["age", "height", "name", "bool"])
Fill all null values with 50 for numeric columns.
df.na.fill(50).show()
# +---+------+-----+----+
# |age|height| name|bool|
# +---+------+-----+----+
# | 10| 80.5|Alice|NULL|
# | 5| 50.0| Bob|NULL|
# | 50| 50.0| Tom|NULL|
# | 50| 50.0| NULL|true|
# +---+------+-----+----+
Fill all null values with False for boolean columns.
df.na.fill(False).show()
# +----+------+-----+-----+
# | age|height| name| bool|
# +----+------+-----+-----+
# | 10| 80.5|Alice|false|
# | 5| NULL| Bob|false|
# |NULL| NULL| Tom|false|
# |NULL| NULL| NULL| true|
# +----+------+-----+-----+
Fill null values with 50 for age and "unknown" for name.
df.na.fill({'age': 50, 'name': 'unknown'}).show()
# +---+------+-------+----+
# |age|height| name|bool|
# +---+------+-------+----+
# | 10| 80.5| Alice|NULL|
# | 5| NULL| Bob|NULL|
# | 50| NULL| Tom|NULL|
# | 50| NULL|unknown|true|
# +---+------+-------+----+
Fill all null values with "Spark" for the name column.
df.na.fill(value='Spark', subset='name').show()
# +----+------+-----+----+
# | age|height| name|bool|
# +----+------+-----+----+
# | 10| 80.5|Alice|NULL|
# | 5| NULL| Bob|NULL|
# |NULL| NULL| Tom|NULL|
# |NULL| NULL|Spark|true|
# +----+------+-----+----+