MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / apply

Method apply

src/danfojs-base/core/frame.ts:2535–2576  ·  view source on GitHub ↗

* Apply a function along an axis of the DataFrame. To apply a function element-wise, use `applyMap`. * Objects passed to the function are Series values whose * index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1) * @param callable Function to apply to ea

(callable: any, options?: { axis?: 0 | 1 })

Source from the content-addressed store, hash-verified

2533 * ```
2534 */
2535 apply(callable: any, options?: { axis?: 0 | 1 }): DataFrame | Series {
2536 const { axis } = { axis: 1, ...options }
2537
2538 if ([0, 1].indexOf(axis) === -1) {
2539 throw Error(`ParamError: axis must be 0 or 1`);
2540 }
2541
2542 const valuesForFunc = this.$getDataByAxisWithMissingValuesRemoved(axis)
2543
2544 const result = valuesForFunc.map(row => {
2545 return callable(row)
2546 })
2547
2548 if (axis === 0) {
2549 if (utils.is1DArray(result)) {
2550 return new Series(result, {
2551 index: [...this.columns]
2552 })
2553 } else {
2554 return new DataFrame(result, {
2555 index: [...this.columns],
2556 columns: [...this.columns],
2557 dtypes: [...this.dtypes],
2558 config: { ...this.config }
2559 })
2560 }
2561 } else {
2562 if (utils.is1DArray(result)) {
2563 return new Series(result, {
2564 index: [...this.index]
2565 })
2566 } else {
2567 return new DataFrame(result, {
2568 index: [...this.index],
2569 columns: [...this.columns],
2570 dtypes: [...this.dtypes],
2571 config: { ...this.config }
2572 })
2573 }
2574 }
2575
2576 }
2577
2578 /**
2579 * Apply a function to a Dataframe values element-wise.

Callers

nothing calls this directly

Calls 4

indexOfMethod · 0.80
is1DArrayMethod · 0.80
mapMethod · 0.65

Tested by

no test coverage detected