| description | Replaces values in a DataFrame with specified values |
|---|
danfo.DataFrame.replace(oldValue, newValue, options)
| Parameters | Type | Description | Default |
|---|---|---|---|
| oldValue | String, boolean, Number | The value you want to replace | |
| newValue | String, boolean, Number | The new value you want to replace the old value with | |
| options | Object | columns: Array. An array of column names to replace, If not specified, replace all columns. inplace: Boolean indicating whether to perform the operation inplace or not. Defaults to false |
{inplace: false} |
{% tabs %} {% tab title="Node" %}
const dfd = require("danfojs-node")
let data = {
"Col1": [10, 45, 56, 10],
"Col2": [23, 20, 10, 24]
}
let df = new dfd.DataFrame(data)
let df_rep = df.replace(10, -999, { columns: ["Col1"] })
df_rep.print()
{% endtab %}
{% tab title="Browser" %}
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Output" %}
βββββ€ββββββββββββββββββββ€ββββββββββββββββββββ
β β Col1 β Col2 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 0 β -999 β 23 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 1 β 45 β 20 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 2 β 56 β 10 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 3 β -999 β 24 β
βββββ§ββββββββββββββββββββ§ββββββββββββββββββββ
{% endtab %} {% endtabs %}
If a column name is not specified, replace works on all columns:
{% tabs %} {% tab title="Node" %}
const dfd = require("danfojs-node")
let data = [["A", "A", "A", "B"], ["B", "C", "C", "D"]]
let df = new dfd.DataFrame(data)
//replace value in all column
let df_rep = df.replace("A", "BOY")
df_rep.print(){% endtab %}
{% tab title="Browser" %}
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Output" %}
ββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ
β β 0 β 1 β 2 β 3 β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 0 β BOY β BOY β BOY β B β
ββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 1 β B β C β C β D β
ββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ{% endtab %} {% endtabs %}