fill (DataFrameNaFunctions) s
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 |
|---|---|---|
| int, float, str, bool, or dict | The value to replace null values with. If a dict is provided, |
| str, tuple, or list, optional | Column names to consider. Columns in |
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|
# +----+------+-----+----+