Series.set_index

Assign new Index to Series

danfo.series.set_index(kwargs) [source]

Parameters

Type

Description

Default

kwargs["index"]

Array

index to replace the former index

kwargs["inplace"]

Boolean

return new series or not

false

Returns: Series

Example

Node
Browser
Node
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)
let sf_new = sf.set_index({ "index": ["one", "two", "three"] })
sf_new.print()
Browser
Output
Output
╔═══════╤══════════════════════╗
║ │ 0 ║
╟───────┼──────────────────────╢
║ one │ {"alpha":"A","cou... ║
╟───────┼──────────────────────╢
║ two │ {"alpha":"B","cou... ║
╟───────┼──────────────────────╢
║ three │ {"alpha":"C","cou... ║
╚═══════╧══════════════════════╝
Node
Browser
Node
const dfd = require("danfojs-node")
let data = ["Humans","Life","Meaning","Fact","Truth"]
let sf = new dfd.Series(data)
let sf_new = sf.set_index({ "index": ["H", "L", "M","F","T"] })
sf_new.print()
Browser
Output
Output
╔═══╤══════════════════════╗
║ │ 0 ║
╟───┼──────────────────────╢
║ H │ Humans ║
╟───┼──────────────────────╢
║ L │ Life ║
╟───┼──────────────────────╢
║ M │ Meaning ║
╟───┼──────────────────────╢
║ F │ Fact ║
╟───┼──────────────────────╢
║ T │ Truth ║
╚═══╧══════════════════════╝

set index without creating a new series by using inplace = true

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