snowflake.snowpark.DataFrameNaFunctions.replace¶
- DataFrameNaFunctions.replace(to_replace: Union[None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict, Iterable[Union[None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict]], Dict[Union[None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict], Union[None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict]]], value: Union[None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict, Iterable[Union[None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict]]] = None, subset: Optional[Union[str, Iterable[str]]] = None, *, include_decimal: bool = False) DataFrame[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.30.0/snowpark-python/src/snowflake/snowpark/dataframe_na_functions.py#L499-L737)¶
- Returns a new DataFrame that replaces values in the specified columns. - Parameters:
- to_replace – A scalar value, or a list of values or a - dictthat associates the original values with the replacement values. If- to_replaceis a- dict,- valueand- subsetare ignored. To replace a null value, use- Nonein- to_replace. To replace a NaN value, use- float("nan")in- to_replace. If- to_replaceis empty, the method returns the original DataFrame.
- value – A scalar value, or a list of values for the replacement. If - valueis a list,- valueshould be of the same length as- to_replace. If- valueis a scalar and- to_replaceis a list, then- valueis used as a replacement for each item in- to_replace.
- subset – A list of the names of columns in which the values should be replaced. If - colsis not provided or- None, the replacement will be applied to all columns. If- colsis empty, the method returns the original DataFrame.
- include_decimal – Whether to allow - Decimalvalues to replace- IntegerTypeand- FloatTypevalues.
 
 - Examples: - >>> df = session.create_dataframe([[1, 1.0, "1.0"], [2, 2.0, "2.0"]], schema=["a", "b", "c"]) >>> # replace 1 with 3 in all columns >>> df.na.replace(1, 3).show() ------------------- |"A" |"B" |"C" | ------------------- |3 |3.0 |1.0 | |2 |2.0 |2.0 | ------------------- >>> # replace 1 with 3 and 2 with 4 in all columns >>> df.na.replace([1, 2], [3, 4]).show() ------------------- |"A" |"B" |"C" | ------------------- |3 |3.0 |1.0 | |4 |4.0 |2.0 | ------------------- >>> # replace 1 with 3 and 2 with 3 in all columns >>> df.na.replace([1, 2], 3).show() ------------------- |"A" |"B" |"C" | ------------------- |3 |3.0 |1.0 | |3 |3.0 |2.0 | ------------------- >>> # the following line intends to replaces 1 with 3 and 2 with 4 in all columns >>> # and will give [Row(3, 3.0, "1.0"), Row(4, 4.0, "2.0")] >>> df.na.replace({1: 3, 2: 4}).show() ------------------- |"A" |"B" |"C" | ------------------- |3 |3.0 |1.0 | |4 |4.0 |2.0 | ------------------- >>> # the following line intends to replace 1 with "3" in column "a", >>> # but will be ignored since "3" (str) doesn't match the original data type >>> df.na.replace({1: "3"}, ["a"]).show() ------------------- |"A" |"B" |"C" | ------------------- |1 |1.0 |1.0 | |2 |2.0 |2.0 | ------------------- - Note - If the type of a given value in - to_replaceor- valuedoesn’t match the column data type (e.g. a- floatfor- StringTypecolumn), this replacement will be skipped in this column. Especially,- intcan replace or be replaced in a column with- FloatTypeor- DoubleType, but- floatcannot replace or be replaced in a column with- IntegerTypeor- LongType.
- Nonecan replace or be replaced in a column with any data type.
 - See also