DataFrame.rename

Change axis labels. Object values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error.

danfo.DataFrame.rename(mapper, options)

ParametersTypeDescriptionDefault

mapper

Object

Labels and transformations to apply to that axis’ values.

options

Object

axis: row=0, columns=1.

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

{

axis : 1,

inplace : false

}

Examples

Rename columns

By setting inplace to true, the original DataFrame is modified and nothing is returned. To not modify the original DataFrame and return a new one, set inplace to false or leave it as default.

const dfd = require("danfojs-node")

let data = {
    "A": [-20, 30, 47.3],
    "B": [34, -4, 5],
    "C": [20, 2, 30]
}


let df = new dfd.DataFrame(data)
df.rename({ "A": "new_name" }, { inplace: true })
df.print()
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ new_name          │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0          │ -20               │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1          │ 30                │ -4                │ 2                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2          │ 47.3              │ 5                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

Rename more the one column at time

const dfd = require("danfojs-node")

let data = { "A": [-20, 30, 47.3],
             "B": [34, -4, 6],
             "C": [20, 2, 30] }


let df = new dfd.DataFrame(data)

df = df.rename({ A: "new_name", C: "new_c" })
df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ new_name          │ B                 │ new_c             ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ -20               │ 34                │ 20                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ 30                │ -4                │ 20                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ 47.3              │ 5                 │ 30                ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝

Rename index by labels

const dfd = require("danfojs-node")

let data = {
    "A": [-20, 30, 47.3],
    "B": [34, -4, 6],
    "C": [20, 2, 30]
}


let df = new dfd.DataFrame(data, { index: ["a", "b", "c"] })
df = df.rename({ "a": 0 }, { axis: 0 })
df.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ A                 │ B                 │ C                 ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ -20               │ 34                │ 20                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ b │ 30                │ -4                │ 20                ║
╟───┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ 47.3              │ 5                 │ 30                ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝

Last updated