danfo.DataFrame.set_index(kwargs) [source]
Parameters | Type | Description | Default |
kwargs | Object | { key: This key can be either a single column name or a single array of the same length as the calling DataFrame, drop: Delete columns to be used as the new index. inplace: specify whether to perform the operation to the row/column with/without creating a new DataFrame } | {drop: true, inplace:false} |
Returns:
return DataFrame
const dfd = require("danfojs-node")let data = { "A": [-20, 30, 47.3],"B": [34, -4, 5, 6],"C": [20, 2, 3, 30] }let df = new dfd.DataFrame(data, {index: ["a", "b", "a"]})df.print()df.set_index({key: "A", inplace: true})df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ -20 │ 34 │ 20 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ b │ 30 │ -4 │ 20 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ 47.3 │ 5 │ 30 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝//after setting index╔══════╤═══════════════════╤═══════════════════╗║ │ B │ C ║╟──────┼───────────────────┼───────────────────╢║ -20 │ 34 │ 20 ║╟──────┼───────────────────┼───────────────────╢║ 30 │ -4 │ 20 ║╟──────┼───────────────────┼───────────────────╢║ 47.3 │ 5 │ 30 ║╚══════╧═══════════════════╧═══════════════════╝
const dfd = require("danfojs-node")let data = { "A": [-20, 30, 47.3],"B": [34, -4, 5, 6],"C": [20, 2, 3, 30] }let df = new dfd.DataFrame(data)df.print()let new_index = ["a", "b", "a"]df.set_index({key: new_index, inplace: true})df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 0 │ -20 │ 34 │ 20 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 1 │ 30 │ -4 │ 20 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ 2 │ 47.3 │ 5 │ 30 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝//after setting the index╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗║ │ A │ B │ C ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ -20 │ 34 │ 20 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ b │ 30 │ -4 │ 20 ║╟───┼───────────────────┼───────────────────┼───────────────────╢║ a │ 47.3 │ 5 │ 30 ║╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
Note: To reset an index to the default values, use the DataFrame.reset_index.