DataFrame.addColumn
Add new column to a DataFrame
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.
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()
βββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ
β β 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 β
βββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ
Add Series as a new column to DataFrame
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()
βββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββββββ
β β A β B β C β D β
βββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 0 β 30 β 34 β 20 β 1 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 1 β 1 β 4 β 20 β 2 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 2 β 2 β 5 β 30 β 3 β
βββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ’
β 3 β 3 β 6 β 40 β 4 β
βββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββββββ
Last updated