bpd.DataFrame.set_index

DataFrame.set_index(keys, drop=True)[source]

Set the DataFrame index using existing columns.

Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index replaces the existing index.

Parameters:
  • keys (label or array-like or list of labels/arrays) – This parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses Series, Index and np.ndarray.
  • drop (bool, default True) – Delete columns to be used as the new index.
Returns:

Data frame with changed row labels.

Return type:

DataFrame

Raises:

KeyError – If keys not found in columns.

Examples

>>> df = bpd.DataFrame().assign(name=['Sally', 'George', 'Bill', 'Ann'],
...                             age=[21, 25, 18, 28],
...                             height_cm=[161, 168, 171, 149])
>>> df.set_index('name')
        age  height_cm
name
Sally    21        161
George   25        168
Bill     18        171
Ann      28        149