Skip to content

Latest commit

 

History

History
125 lines (102 loc) · 7.28 KB

danfo.dataframe.addcolumn.md

File metadata and controls

125 lines (102 loc) · 7.28 KB
description
Add new column to a DataFrame

DataFrame.addColumn

danfo.DataFrame.addColumn(column**,** values**,** options)

Parameters Type Description Default
column String Name of the column to add.
values Series**,** Array New values to add
options Object inplace: Whether to perform the operation inplace or not. Default to false.

Returns:

Examples

Add Array as a new column to DataFrame

New columns get added at the end of the DataFrame.

{% tabs %} {% tab title="Node" %}

const dfd = require("danfojs-node")

let data = {
    "A": [30, 1, 2, 3],
    "B": [34, 4, 5, 6],
    "C": [20, 20, 30, 40]
}

let df = new dfd.DataFrame(data)
df.print()

let new_col = [1, 2, 3, 4]
df.addColumn("D", new_col, { inplace: true });

df.print()

{% endtab %}

{% tab title="Browser" %}

{% endtab %} {% endtabs %}

{% tabs %} {% tab title="Output" %}

╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ A                 │ B                 │ C                 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ 30                │ 34                │ 20                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ 1                 │ 4                 │ 20                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ 2                 │ 5                 │ 30                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ 3                 │ 6                 │ 40                ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝

╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ A                 │ B                 │ C                 │ D                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ 30                │ 34                │ 20                │ 1                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ 1                 │ 4                 │ 20                │ 2                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ 2                 │ 5                 │ 30                │ 3                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ 3                 │ 6                 │ 40                │ 4                 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

{% endtab %} {% endtabs %}

Add Series as a new column to DataFrame

{% tabs %} {% tab title="Node" %}

const dfd = require("danfojs-node")

let data = {
    "A": [30, 1, 2, 3],
    "B": [34, 4, 5, 6],
    "C": [20, 20, 30, 40]
}

let df = new dfd.DataFrame(data)
df.print()

let s = new dfd.Series([1, 2, 3, 4])
df.addColumn("D", s, { inplace: true });

df.print()

{% endtab %}

{% tab title="Browser" %}

{% endtab %} {% endtabs %}

{% tabs %} {% tab title="Output" %}

╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ A                 │ B                 │ C                 │ D                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ 30                │ 34                │ 20                │ 1                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ 1                 │ 4                 │ 20                │ 2                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ 2                 │ 5                 │ 30                │ 3                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ 3                 │ 6                 │ 40                │ 4                 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

{% endtab %} {% endtabs %}