w3resource

Pandas DataFrame: set_index() function

DataFrame - set_index() function

The set_index() function is used to 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 can replace the existing index or expand on it.

Syntax:

DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False)

Parameters:

Name Description Type / Default Value Required / Optional
keys 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, np.ndarray, and instances of Iterator. label or array-like or list of labels/arrays Required
drop  Delete columns to be used as the new index. bool
Default Value: True
Required
append  Whether to append columns to existing index. bool
Default Value: False
Required
inplace  Modify the DataFrame in place (do not create a new object). bool
Default Value: False
Required
verify_integrity  Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method. bool
Default Value: False
Required

Returns: DataFrame
Changed row labels.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - set_axis() function
Next: DataFrame - take() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/dataframe/dataframe-set_index.php