DataFrame.apply

Apply a function to each element or along a specified axis of a DataFrame. Supports JavaScipt functions when axis is not specified, and accepts Tensorflow functions when axis is specified.

danfo.DataFrame.apply(kwargs) [source]

Parameters

Type

Description

Default

kwargs

Object

{callable: Function to call on each element,

axis: undefined: if undefined, then any JavaScript function

is accepted and will be applied element-wise.

0: Apply along row/index axis

1: Apply across columns axis

}

undefined

Returns:

return DataFrame

Examples

Apply a JavaScript function to all elements in DataFrame

The apply function calls a JavaScript function on every element of the DataFrame when the axis is not specified.

Node
Browser
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(x) {
return x + 20
}
let df_new = df.apply({callable: sum_vals })
df_new.print()
Browser
Output
Output
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ A │ B │ C ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ 21 │ 22 │ 23 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ 24 │ 25 │ 26 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ 40 │ 50 │ 60 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ 59 │ 109 │ 98 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Node
Browser
Node
const dfd = require("danfojs-node")
let data = [{ short_name: ["NG", "GH", "EGY", "SA"] },
{ long_name: ["Nigeria", "Ghana", "Eqypt", "South Africa"] }]
let df = new dfd.DataFrame(data)
function lower(x) {
return x.toLowerCase()
}
let df_new = df.apply({ callable: lower })
df_new.print()
Browser
Output
Output
╔═══╤═══════════════════╤═══════════════════╗
║ │ short_name │ long_name ║
╟───┼───────────────────┼───────────────────╢
║ 0 │ ng │ nigeria ║
╟───┼───────────────────┼───────────────────╢
║ 1 │ gh │ ghana ║
╟───┼───────────────────┼───────────────────╢
║ 2 │ egy │ eqypt ║
╟───┼───────────────────┼───────────────────╢
║ 3 │ sa │ south africa ║
╚═══╧═══════════════════╧═══════════════════╝

Apply Tensorflow function element-wise

You can call any compatible Tensorflow function on a DataFrame across a specified axis. For functions that operate element-wise and returns the same shape as the original DataFrame, you must specify an axis of 0.

Node
Browser
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(x) {
return x.logSigmoid()
}
let df_new = df.apply({axis: 0, callable: sum_vals })
df_new.print()
Browser
Output
Output
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ A │ B │ C ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ -0.3132616579... │ -0.1269280463... │ -0.0485873296... ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ -0.0181499607... │ -0.0067153489... │ -0.0024756519... ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ -2.0611536921... │ -9.3576229122... │ -4.2483541311... ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ -1.1548223864... │ -2.2273639090... │ -1.3336148713... ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝

Apply Tensorflow function along column axis

Node
Browser
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(x) {
return x.sum()
}
let df_new = df.apply({axis: 1, callable: sum_vals })
df_new.print()
Browser
Output
Output
╔═══╤══════════════════════╗
║ │ 0 ║
╟───┼──────────────────────╢
║ A │ 64 ║
╟───┼──────────────────────╢
║ B │ 126 ║
╟───┼──────────────────────╢
║ C │ 127 ║
╚═══╧══════════════════════╝