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.

Syntax:

Parameters:

  • 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.

Returns:

It will change the row labels as the output.

Example1:

This example shows how to set the index:

Output:

	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:

Output:

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

Example2:

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

Output:

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

Example3:

It creates a MultiIndex using an Index and a column:

Output:

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

Example4:

Create a MultiIndex using two Series:

Output:

		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

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA