replace (DataFrame)
Returns a new DataFrame replacing a value with another value. DataFrame.replace and DataFrameNaFunctions.replace are aliases of each other. Values to_replace and value must have the same type and can only be numerics, booleans, or strings. Value can have None. When replacing, the new value will be cast to the type of the existing column.
Syntax
replace(to_replace: Union["LiteralType", List["LiteralType"], Dict["LiteralType", "OptionalPrimitiveType"]], value: Optional[Union["OptionalPrimitiveType", List["OptionalPrimitiveType"]]] = _NoValue, subset: Optional[List[str]] = None)
Parameters
Parameter | Type | Description |
|---|---|---|
| bool, int, float, string, list or dict | the value to be replaced. If the value is a dict, then |
| bool, int, float, string or None, optional | The replacement value must be a bool, int, float, string or None. If |
| list, optional | optional list of column names to consider. Columns specified in subset that do not have matching data types are ignored. |
Returns
DataFrame: DataFrame with replaced values.
Examples
df = spark.createDataFrame([
(10, 80, "Alice"),
(5, None, "Bob"),
(None, 10, "Tom"),
(None, None, None)],
schema=["age", "height", "name"])
df.na.replace(10, 20).show()
# +----+------+-----+
# | age|height| name|
# +----+------+-----+
# | 20| 80|Alice|
# | 5| NULL| Bob|
# |NULL| 20| Tom|
# |NULL| NULL| NULL|
# +----+------+-----+
df.na.replace('Alice', None).show()
# +----+------+----+
# | age|height|name|
# +----+------+----+
# | 10| 80|NULL|
# | 5| NULL| Bob|
# |NULL| 10| Tom|
# |NULL| NULL|NULL|
# +----+------+----+
df.na.replace(['Alice', 'Bob'], ['A', 'B'], 'name').show()
# +----+------+----+
# | age|height|name|
# +----+------+----+
# | 10| 80| A|
# | 5| NULL| B|
# |NULL| 10| Tom|
# |NULL| NULL|NULL|
# +----+------+----+