Series.setIndex
Assign new Index to Series
danfo.series.setIndex**(options)**
Parameter
Type
Description
Default
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
Node
Browser
1
const dfd = require("danfojs-node")
2
3
let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }]
4
let sf = new dfd.Series(data)
5
sf.print()
6
7
let sf_new = sf.setIndex(["one", "two", "three"])
8
sf_new.print()
Copied!
1
Copied!
Output
1
╔═══╤═════════════════════════╗
2
║ 0 │ {"alpha":"A","count":1} ║
3
╟───┼─────────────────────────╢
4
║ 1 │ {"alpha":"B","count":2} ║
5
╟───┼─────────────────────────╢
6
║ 2 │ {"alpha":"C","count":3} ║
7
╚═══╧═════════════════════════╝
8
9
╔═══════╤═════════════════════════╗
10
║ one │ {"alpha":"A","count":1} ║
11
╟───────┼─────────────────────────╢
12
║ two │ {"alpha":"B","count":2} ║
13
╟───────┼─────────────────────────╢
14
║ three │ {"alpha":"C","count":3} ║
15
╚═══════╧═════════════════════════╝
Copied!
Node
Browser
1
const dfd = require("danfojs-node")
2
3
let data = ["Humans","Life","Meaning","Fact","Truth"]
4
let sf = new dfd.Series(data)
5
let sf_new = sf.setIndex(["H", "L", "M","F","T"])
6
sf_new.print()
Copied!
1
Copied!
Output
1
╔═══╤═════════╗
2
║ H │ Humans ║
3
╟───┼─────────╢
4
║ L │ Life ║
5
╟───┼─────────╢
6
║ M │ Meaning ║
7
╟───┼─────────╢
8
║ F │ Fact ║
9
╟───┼─────────╢
10
║ T │ Truth ║
11
╚═══╧═════════╝
Copied!

Set index in-place

Node
Browser
1
const dfd = require("danfojs")
2
3
let data = [1, 2, 3, 4, 5, 6]
4
let sf = new dfd.Series(data)
5
sf.setIndex(["one", "two", "three", "four", "five", "six"], { inplace: true })
6
sf.print()
Copied!
1
Copied!
Output
1
╔═══════╤═══╗
2
║ one │ 1 ║
3
╟───────┼───╢
4
║ two │ 2 ║
5
╟───────┼───╢
6
║ three │ 3 ║
7
╟───────┼───╢
8
║ four │ 4 ║
9
╟───────┼───╢
10
║ five │ 5 ║
11
╟───────┼───╢
12
║ six │ 6 ║
13
╚═══════╧═══╝
Copied!
Last modified 13d ago
Copy link