Replace values¶
fast step NLP • text
Replace specified values in a column with new ones.
This function enables the replacement of specified values in a column with new ones. It takes a mapping object, where each key-value pair represents an "old value" -> "new value" transformation. The function scans the column for values that match any of the keys in the mapping object and replaces them with their corresponding new values.
This function is case-sensitive and performs exact matches.
For numeric replacements, it's important to note that keys in the mapping object need to be strings, even for numeric columns. The function will internally convert these keys into numbers for comparison.
Usage¶
The following are the step's expected inputs and outputs and their specific types.
replace_values(original: category|text|number|list, {"param": value}) -> (replaced: column)
where the object {"param": value}
is optional in most cases and if present may contain any of the parameters described in the
corresponding section below.
Example¶
To change specific names in a column of texts from "pedro" to "pablo" and "maria" to "mariana":
replace_values(ds.names, {
"pedro": "pablo",
"maria": "mariana"
}) -> (ds.replaced)
More examples
To change specific numbers in a column of numerical values from "20" to 99 and "30" to 100:
replace_values(ds.age, {
"20": 99,
"30": 100
}) -> (ds.replaced)
To replace null values in a column with a string:
replace_values(ds.column, {
"": "something not null"
}) -> (ds.replaced)
To replace a specific numerical value in a column with null using the keyword null:
replace_values(ds.age, {
"30": null
}) -> (ds.replaced)
To replace a specific numerical value in a column with null using the string "null":
replace_values(ds.age, {
"30": "null"
}) -> (ds.replaced)
Inputs¶
original: column:category|text|number|list
A column containing text-like values.
Outputs¶
replaced: column
The output column's data type will depend on the input and specified parameters:
text
: if input is text and parameter"as_category": false
category
: if input is not a column of lists and"as_category": true
list
: if input is a column of lists,list
of the same kind as the input.