| description | Apply a function to each element or along a specified axis of a DataFrame. |
|---|
danfo.DataFrame.apply(callable, options)
| Parameters | Type | Description | Default |
|---|---|---|---|
| callable | Function | Function to apply to each column or row | |
| options | Object | axis: 0 or 1. If 0, compute the power column-wise, if 1, row-wise | {axis: 1} |
{% hint style="info" %}
Note that the specified function passed to apply will be called with an array of the values across the specified axis.
{% endhint %}
{% tabs %} {% tab title="Node" %}
const dfd = require("danfojs-node")
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
let cols = ["A", "B", "C"]
let df = new dfd.DataFrame(data, { columns: cols })
function sum_vals(col) {
return col.reduce((a, b) => a + b, 0);
}
let df_new = df.apply(sum_vals, { axis: 1 })
df_new.print(){% endtab %}
{% tab title="Browser" %}
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Output" %}
βββββ€ββββββ
β 0 β 6 β
βββββΌββββββ’
β 1 β 15 β
βββββΌββββββ’
β 2 β 90 β
βββββΌββββββ’
β 3 β 206 β
βββββ§ββββββ
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Node" %}
const dfd = require("danfojs-node")
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
let cols = ["A", "B", "C"]
let df = new dfd.DataFrame(data, { columns: cols })
function sum_vals(col) {
return col.reduce((a, b) => a + b, 0);
}
let df_new = df.apply(sum_vals, { axis: 0 })
df_new.print(){% endtab %}
{% tab title="Browser" %}
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Output" %}
βββββ€ββββββ
β A β 64 β
βββββΌββββββ’
β B β 126 β
βββββΌββββββ’
β C β 127 β
βββββ§ββββββ
{% endtab %} {% endtabs %}