DataFrame.apply
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}
Examples
Apply a function along default axis 1 (columns)
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()Apply a function along axis 0 (row)
Last updated
Was this helpful?