Powered By GitBook
Series.set_index
Assign new Index to Series
danfo.series.set_index(options) [source]
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
let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }]
3
let sf = new dfd.Series(data)
4
sf.print()
5
6
let sf_new = sf.set_index({ "index": ["one", "two", "three"] })
7
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.set_index({ "index": ["H", "L", "M","F","T"] })
6
sf_new.print()
Copied!
1
Copied!
Output
1
╔═══╤══════════════════════╗
2
║ │ 0 ║
3
╟───┼──────────────────────╢
4
║ H │ Humans ║
5
╟───┼──────────────────────╢
6
║ L │ Life ║
7
╟───┼──────────────────────╢
8
║ M │ Meaning ║
9
╟───┼──────────────────────╢
10
║ F │ Fact ║
11
╟───┼──────────────────────╢
12
║ T │ Truth ║
13
╚═══╧══════════════════════╝
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.set_index({ index: ["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
╚═══════╧═══╝
14
Copied!
Last modified 13d ago
Copy link