Javatpoint Logo
Javatpoint Logo

Set Index

Pandas set index() is used to set a List, Series or DataFrame as index of a Data Frame. We can set the index column while making a data frame. But sometimes a data frame is made from two or more data frames and then index can be changed using this method.



  • keys: Refers to label or array-like or list of labels/arrays

It can be either a single column key, a single array of the same length as the calling DataFrame, or also a list that contains an arbitrary combination of column keys and arrays.

  • drop: Returns the boolean value, default value is True. Used to delete the columns that are to be used as the new index.
  • append: Returns the boolean value, default value is False.

It checks whether append the columns to an existing index.

  • inplace: Returns the boolean value, default value False.

It is used to modify the DataFrame in place. We don't need to create a new object.

  • verify_integrity: Returns the boolean value, default value False.

It checks the new index for duplicate values. Otherwise, it will defer the check until necessary. It also set it to False that will improve the performance of this method.


It will change the row labels as the output.


This example shows how to set the index:


	Name	Age	id
0	William	32	105
1	Phill	38	132
2	Parker	41	134
3	Smith	36	127

Now, we have to set the index to create the 'month' column:


         Age  id
William  32  105
Phill    38  132
Parker   41  134
Smith    36  127


Create a MultiIndex using columns 'Age' and 'Name':


	Name	id
32	William	105
38	Phill	132
41	Parker	134
36	Smith	127


It creates a MultiIndex using an Index and a column:


		Age	id
1	William	32	105
2	Phill	38	132
3	Parker	41	134
4	Smith	36	127


Create a MultiIndex using two Series:


		Name	Age	id
1	1	William	32	105
2	4	Phill	38	132
3	9	Parker	41	134
4	16	Smith	36	127

Next TopicPandas NumPy

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA