# DataFrame.setIndex

danfo.DataFrame.setIndex(options) \[[source](https://github.com/opensource9ja/danfojs/blob/cf5c7ae3a009458e61eedd18d9c9b5b6b10d5276/danfojs/src/core/frame.js#L125)]

| Parameters | Type   | Description                                                                                                                                                                                                                                                                                                                                                             | Default                                  |
| ---------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| options    | Object | <p>{</p><p><strong>index</strong>: An array of index values to set.</p><p><strong>column</strong>: A column name to set the index to.</p><p><strong>drop</strong>: Whether to drop the column whose index was set. Defaults to false.</p><p><strong>inplace</strong>: Boolean indicating whether to perform the operation inplace or not. Defaults to false</p><p>}</p> | {**drop**: false, \*\*inplace:\*\*false} |

## **Examples**

### **Setting index to a column in the DataFrame**

{% tabs %}
{% tab title="Node" %}

```javascript
const dfd = require("danfojs-node")

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


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

df.setIndex({column: "A", inplace: true})
df.print()
```

{% endtab %}

{% tab title="Browser" %}

```
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Output" %}

```
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ a          │ -20               │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ b          │ 30                │ 5                 │ 3                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ c          │ 47.3              │ 6                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ -20        │ -20               │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 30         │ 30                │ 5                 │ 3                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 47.3       │ 47.3              │ 6                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
```

{% endtab %}
{% endtabs %}

### **Setting index to a column in the DataFrame and dropping the column**

{% tabs %}
{% tab title="Node" %}

```javascript
const dfd = require("danfojs-node")

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


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

df.setIndex({column: "A", drop: true, inplace: true})
df.print()
```

{% endtab %}

{% tab title="Browser" %}

```
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Output" %}

```
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ a          │ -20               │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ b          │ 30                │ 5                 │ 3                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ c          │ 47.3              │ 6                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

╔════════════╤═══════════════════╤═══════════════════╗
║            │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────╢
║ -20        │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────╢
║ 30         │ 5                 │ 3                 ║
╟────────────┼───────────────────┼───────────────────╢
║ 47.3       │ 6                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╝
```

{% endtab %}
{% endtabs %}

### **Set index to an array of the same length**

{% tabs %}
{% tab title="Node" %}

```javascript
const dfd = require("danfojs-node")

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


let df = new dfd.DataFrame(data)
df.print()

let new_index = ["a", "b", "c"]
df.setIndex({index: new_index, inplace: true})
df.print()
```

{% endtab %}

{% tab title="Browser" %}

```
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Output" %}

```
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0          │ -20               │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1          │ 30                │ -5                │ 3                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2          │ 47.3              │ 6                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ a          │ -20               │ 34                │ 20                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ b          │ 30                │ -5                │ 3                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ c          │ 47.3              │ 6                 │ 30                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
```

{% endtab %}
{% endtabs %}

**Note:** To reset an index to the default values, use the [DataFrame.resetIndex](https://danfo.jsdata.org/api-reference/dataframe/dataframe.reset_index).
