Teradata Package for Python Function Reference | 20.00 - replace - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.
Teradata® Package for Python Function Reference - 20.00
- Deployment
- VantageCloud
- VantageCore
- Edition
- Enterprise
- IntelliFlex
- VMware
- Product
- Teradata Package for Python
- Release Number
- 20.00.00.03
- Published
- December 2024
- ft:locale
- en-US
- ft:lastEdition
- 2024-12-19
- dita:id
- TeradataPython_FxRef_Enterprise_2000
- lifecycle
- latest
- Product Category
- Teradata Vantage
- teradataml.dataframe.sql.DataFrameColumn.replace = replace(self, to_replace, value=None)
- DESCRIPTION:
Function replaces every occurrence of "to_replace" in the column
with the "value". Use this function to replace a value from string.
Note:
The function replaces value in a string column only when it
matches completely. If you want to replace a value in a string
column with only a portion of the string, then use function
oreplace.
PARAMETERS:
to_replace:
Required Argument.
Specifies a ColumnExpression or a literal that the function
searches for values in the Column. Use ColumnExpression when
you want to match the condition based on a DataFrameColumn
function, else use literal.
Note:
Only ColumnExpressions generated from DataFrameColumn
functions are supported. BinaryExpressions are not supported.
Example: Consider teradataml DataFrame has two columns COL1, COL2.
df.COL1.abs() is supported but df.COL1 == df.COL2 is not
supported.
Supported column types: CHAR, VARCHAR, FLOAT, INTEGER, DECIMAL
Types: ColumnExpression OR int OR float OR str OR dict
value:
Required argument when "to_replace" is not a dictionary. Optional otherwise.
Specifies a ColumnExpression or a literal that replaces
the "to_replace" in the column. Use ColumnExpression when
you want to replace based on a DataFrameColumn function, else
use literal.
Notes:
* Argument is ignored if "to_replace" is a dictionary.
* Only ColumnExpressions generated from DataFrameColumn
functions are supported. BinaryExpressions are not supported.
Example: Consider teradataml DataFrame has two columns COL1, COL2.
df.COL1.abs() is supported but df.COL1 == df.COL2 is not
supported.
Supported column types: CHAR, VARCHAR, FLOAT, INTEGER, DECIMAL
Types: ColumnExpression OR int OR float OR str
RAISES:
TeradataMlException
RETURNS:
ColumnExpression
EXAMPLES:
# Load the data to run the example.
>>> load_example_data("teradataml", "chi_sq")
# Create a DataFrame on 'chi_sq' table.
>>> df = DataFrame("chi_sq")
>>> print(df)
dem rep
gender
female 6 9
male 8 5
# Example 1: Create a new column 'new_column' by replacing all the
# occurances of 'male' with 'man' in Column 'gender'.
>>> res = df.assign(new_column = df.gender.replace("male", "man"))
>>> print(res)
dem rep new_column
gender
male 8 5 man
female 6 9 female
# Example 2: Create a new Column 'new_column' by replacing all the
# occurances of 5 with square root of 5 in Column 'rep'.
>>> print(df.assign(new_column = df.rep.replace(5, df.rep.sqrt())))
dem rep new_column
gender
male 8 5 2.236068
female 6 9 9.000000
# Example 3: Create a new Column 'new_column' by replacing all the
# occurances of 5 with square root of 5 and 9 with
# the values of Column 'dem' in Column 'rep'.
>>> print(df.assign(new_column = df.rep.replace({5: df.rep.sqrt(), 9:df.dem})))
dem rep new_column
gender
male 8 5 2.236068
female 6 9 6.000000
# Example 4: Create a new Column 'new_column' by replacing all the
# the values of Column 'rep' with it's square root.
>>> print(df.assign(new_column = df.rep.replace({df.rep: df.rep.sqrt()})))
dem rep new_column
gender
female 6 9 3.000000
male 8 5 2.236068