Series.setIndex

Assign new Index to Series

danfo.series.setIndex**(options)**

ParameterTypeDescriptionDefault

index

Array

new index values

options

Object

inplace: Boolean indicating whether to perform the operation inplace or not. Defaults to false

{

inplace: false

}

Returns: Series

Example

const dfd = require("danfojs-node")

let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }]
let sf = new dfd.Series(data)
sf.print()

let sf_new = sf.setIndex(["one", "two", "three"])
sf_new.print()
╔═══╤═════════════════════════╗
║ 0 │ {"alpha":"A","count":1} ║
╟───┼─────────────────────────╢
║ 1 │ {"alpha":"B","count":2} ║
╟───┼─────────────────────────╢
║ 2 │ {"alpha":"C","count":3} ║
╚═══╧═════════════════════════╝

╔═══════╤═════════════════════════╗
║ one   │ {"alpha":"A","count":1} ║
╟───────┼─────────────────────────╢
║ two   │ {"alpha":"B","count":2} ║
╟───────┼─────────────────────────╢
║ three │ {"alpha":"C","count":3} ║
╚═══════╧═════════════════════════╝
const dfd = require("danfojs-node")

let data = ["Humans","Life","Meaning","Fact","Truth"]
let sf = new dfd.Series(data)
let sf_new = sf.setIndex(["H", "L", "M","F","T"])
sf_new.print()
╔═══╤═════════╗
║ H │ Humans  ║
╟───┼─────────╢
║ L │ Life    ║
╟───┼─────────╢
║ M │ Meaning ║
╟───┼─────────╢
║ F │ Fact    ║
╟───┼─────────╢
║ T │ Truth   ║
╚═══╧═════════╝

Set index in-place

const dfd = require("danfojs")

let data = [1, 2, 3, 4, 5, 6]
let sf = new dfd.Series(data)
sf.setIndex(["one", "two", "three", "four", "five", "six"], { inplace: true })
sf.print()
╔═══════╤═══╗
║ one   │ 1 ║
╟───────┼───╢
║ two   │ 2 ║
╟───────┼───╢
║ three │ 3 ║
╟───────┼───╢
║ four  │ 4 ║
╟───────┼───╢
║ five  │ 5 ║
╟───────┼───╢
║ six   │ 6 ║
╚═══════╧═══╝

Last updated