DataFrame.sortIndex

Sort DataFrame by index

DataFrame.sortIndex(options) [source]

ParametersTypeDescriptionDefault

options

Object

{

ascending: Sorting order.

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

}

{ascending: true, **inplace:**false}

Returns:

**** return DataFrame

Examples

Sort DataFrame by a column in ascending order

const dfd = require("danfojs-node")

let data = [[0, 2, 4, "b"],
            [360, 180, 360, "a"],
            [2, 4, 6, "c"]]

let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"],
                           index: ["b", "a", "c"] })
df.print()

let df2 = df.sortIndex({ ascending: false })
df2.print()
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ col1              │ col2              │ col3              │ col4              ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ b │ 0                 │ 2                 │ 4                 │ b                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ a │ 360               │ 180               │ 360               │ a                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ c │ 2                 │ 4                 │ 6                 │ c                 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝


 //after sorting in descending order

╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║   │ col1              │ col2              │ col3              │ col4              ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ c │ 2                 │ 4                 │ 6                 │ c                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ b │ 0                 │ 2                 │ 4                 │ b                 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ a │ 360               │ 180               │ 360               │ a                 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

Last updated